diff options
author | Keuin <[email protected]> | 2022-03-27 17:40:18 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-03-27 17:40:18 +0800 |
commit | aae1f386456a605c12263e2ba5f09dc36bb180b3 (patch) | |
tree | d125ec6c6ecc0e53389713578374b1f510e65416 /src/config.rs |
Finally, it works.
Further cleanup work is needed.
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..d32c525 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,24 @@ +use std::fs::File; +use std::io::Read; + +use serde_derive::Deserialize; + +#[derive(Deserialize)] +pub struct Config { + pub bot_token: String, + pub log_file: String, + pub db_file: String, +} + +impl Config { + // Read config file. Panic if any error occurs. + pub fn from_file(file_path: &str) -> Config { + let mut file = File::open(file_path) + .unwrap_or_else(|_| panic!("cannot open file {}", file_path)); + let mut config = String::new(); + file.read_to_string(&mut config) + .unwrap_or_else(|_| panic!("cannot read config file {}", file_path)); + return serde_json::from_str(config.as_str()) + .expect("cannot decode config file in JSON"); + } +}
\ No newline at end of file |