diff options
author | Keuin <[email protected]> | 2022-03-28 20:55:19 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-03-28 20:55:19 +0800 |
commit | e2e2f2238f5743340718ed049999dba5f4e4b4f7 (patch) | |
tree | d0b4045defb2d2290d8c16d983f24d843273f020 /src/config.rs | |
parent | 66c7afed4025784834b5a5d743b89c92ffe39bd0 (diff) |
Make some fields in config file optional.
Especially, if log_file is not specified, will not write logs to file.
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index c7549ef..1300e9a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,15 +4,46 @@ use std::io::Read; use serde_derive::Deserialize; use tracing::error; +pub const DEFAULT_LOG_LEVEL: &str = "DEBUG"; + #[derive(Deserialize)] pub struct Config { pub bot_token: String, + #[serde(default = "Config::default_log_file")] pub log_file: String, + #[serde(default = "Config::default_db_file")] pub db_file: String, + #[serde(default = "Config::default_listen")] pub listen: String, + #[serde(default = "Config::default_log_level")] pub log_level: String, + #[serde(default = "Config::default_max_body_size")] + pub max_body_size: u64, +} + +impl Config { + fn default_log_level() -> String { + String::from(DEFAULT_LOG_LEVEL) + } + + fn default_log_file() -> String { + String::new() // empty string means not logging to file + } + + fn default_max_body_size() -> u64 { + 1024 * 16 + } + + fn default_listen() -> String { + String::from("localhost:8080") + } + + fn default_db_file() -> String { + String::from("kimikuri.db") + } } + impl Config { // Read config file. Panic if any error occurs. pub fn from_file(file_path: &str) -> Config { |