diff --git a/src/config.rs b/src/config.rs index bc190ef..e1afe8d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,16 +2,50 @@ use std::path::PathBuf; use serde::Deserialize; #[derive(Deserialize)] +struct RawColors { + pub background: Option, + pub timeline: Option, + pub background_text: Option +} + +#[derive(Clone)] pub struct Colors { pub background: u32, pub timeline: u32, pub background_text: u32 } +impl Default for Colors { + fn default() -> Self { + Colors { + background: 0xff_808080, + timeline: 0xff_a9a9a9, + background_text: 0xff_000000 + } + } +} + +impl From for Colors { + fn from(value: RawColors) -> Self { + let default_colors: Colors = Default::default(); + Colors { + background: value.background.unwrap_or(default_colors.background), + timeline: value.timeline.unwrap_or(default_colors.timeline), + background_text: value.background_text.unwrap_or(default_colors.background_text), + } + } +} + #[derive(Deserialize)] +struct RawConfig { + pub log_path: Option, + pub colors: Option, + pub event_colors: Option>, + pub text_colors: Option> +} + pub struct Config { /// directory, where config is located - #[serde(skip)] pub conf_path: PathBuf, pub log_path: PathBuf, pub colors: Colors, @@ -19,14 +53,10 @@ pub struct Config { pub text_colors: Vec } -impl Config { - pub fn new(conf_path: PathBuf) -> Self { - let conf_dir: PathBuf = conf_path.parent().unwrap().into(); - let colors = Colors { - background: 0xff_808080, - timeline: 0xff_a9a9a9, - background_text: 0xff_000000 - }; +impl Default for Config { + fn default() -> Self { + let conf_path = PathBuf::new(); + let colors: Colors = Default::default(); let event_colors: Vec = vec![ 0xff_97f9f9, 0xff_a4def9, @@ -64,18 +94,46 @@ impl Config { 0xff000000 ]; Config { - conf_path: conf_dir, + conf_path, log_path: PathBuf::from("./logs"), colors, event_colors, text_colors } } +} + +impl From for Config { + fn from(value: RawConfig) -> Self { + let default_config: Config = Default::default(); + let colors: Colors = match value.colors { + Some(raw_colors) => raw_colors.into(), + None => default_config.colors.clone() + }; + Config { + conf_path: default_config.conf_path, + log_path: value.log_path.unwrap_or(default_config.log_path), + colors, + event_colors: value.event_colors.unwrap_or(default_config.event_colors.clone()), + text_colors: value.text_colors.unwrap_or(default_config.text_colors.clone()) + } + } +} + +impl Config { + pub fn new(conf_path: PathBuf) -> Self { + let conf_dir: PathBuf = conf_path.parent().unwrap().into(); + Config { + conf_path: conf_dir, + ..Default::default() + } + } pub fn load(path: PathBuf) -> Self { if let Ok(toml_string) = std::fs::read_to_string(path.clone()) { - let conf = toml::from_str::(&toml_string); - if let Ok(mut conf) = conf { + let conf = toml::from_str::(&toml_string); + if let Ok(raw_conf) = conf { + let mut conf: Config = raw_conf.into(); conf.conf_path = path.parent().unwrap().into(); return conf; }