diff --git a/TODO b/TODO index d890fbd..5d854d4 100644 --- a/TODO +++ b/TODO @@ -3,5 +3,3 @@ feat: command line interface proper config path handling more coloring options -refactor: - remove weird RawConfig struct diff --git a/src/config.rs b/src/config.rs index e1afe8d..17eba1d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,18 +1,12 @@ use std::path::PathBuf; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; -#[derive(Deserialize)] -struct RawColors { - pub background: Option, - pub timeline: Option, - pub background_text: Option -} - -#[derive(Clone)] +#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug)] +#[serde(default)] pub struct Colors { pub background: u32, pub timeline: u32, - pub background_text: u32 + pub background_text: u32, } impl Default for Colors { @@ -20,37 +14,21 @@ impl Default for Colors { Colors { background: 0xff_808080, timeline: 0xff_a9a9a9, - background_text: 0xff_000000 + 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> -} - +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] +#[serde(default)] pub struct Config { /// directory, where config is located + #[serde(skip)] pub conf_path: PathBuf, pub log_path: PathBuf, pub colors: Colors, pub event_colors: Vec, - pub text_colors: Vec + pub text_colors: Vec, } impl Default for Config { @@ -98,24 +76,7 @@ impl Default for Config { 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()) + text_colors, } } } @@ -131,9 +92,7 @@ impl Config { 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(raw_conf) = conf { - let mut conf: Config = raw_conf.into(); + if let Ok(mut conf) = toml::from_str::(&toml_string) { conf.conf_path = path.parent().unwrap().into(); return conf; } diff --git a/src/log.rs b/src/log.rs index 695f1a4..8f0bcbb 100644 --- a/src/log.rs +++ b/src/log.rs @@ -6,10 +6,10 @@ use serde::{Deserialize, Serialize}; use toml::value::{Date, Time}; use crate::config::Config; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct Log { pub date: Date, - pub events: Vec + pub events: Vec, } impl Log { @@ -64,12 +64,12 @@ impl Log { } } -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] pub struct Event { pub name: String, pub start: Time, pub end: Time, - pub finished: bool + pub finished: bool, } impl Event { diff --git a/src/main.rs b/src/main.rs index 9797339..19f0011 100644 --- a/src/main.rs +++ b/src/main.rs @@ -238,15 +238,24 @@ fn main() -> Result<(), Box> { let ui = ui_weak.unwrap(); let prev_event_name = { let log_guard = log.lock().expect("Log shouldn't be used twice"); - let prev_index = log_guard.events.len() - 2; - log_guard.events - .get(prev_index) - .expect("Index is already checked") - .name - .clone() + match log_guard.events.len().checked_sub(2) { + Some(prev_index) => { + let prev_event = log_guard.events + .get(prev_index) + .expect("Index is already checked") + .name + .clone(); + Some(prev_event) + }, + None => None, + } }; - println!("{}", prev_event_name.as_str()); - ui.set_previous_event_name(prev_event_name.to_shared_string()); + if let Some(name) = prev_event_name { + println!("{name}"); + ui.set_previous_event_name(name.to_shared_string()); + } else { + ui.set_previous_event_name(SharedString::new()); + } } });