diff --git a/src/config.rs b/src/config.rs index df47857..742a267 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use parse_duration::parse_duration; +use parse_duration::parse; use std::fmt::Formatter; use std::time::Duration; use std::{env, fs}; @@ -12,7 +12,7 @@ pub struct Config { default = "default_duration", deserialize_with = "deserialize_duration" )] - pub interval: Option, + pub interval: Duration, #[serde(default)] pub colors: ColorConfig, #[serde(default)] @@ -45,7 +45,7 @@ impl Config { impl Default for Config { fn default() -> Self { Self { - interval: Some(Duration::from_secs(60)), + interval: Duration::from_secs(60), colors: ColorConfig::default(), checks: vec![], } @@ -84,11 +84,11 @@ pub enum CheckType { Tcp, } -fn default_duration() -> Option { - Some(Duration::from_secs(60)) +fn default_duration() -> Duration { + Duration::from_secs(60) } -fn deserialize_duration<'de, D>(d: D) -> Result, D::Error> +fn deserialize_duration<'de, D>(d: D) -> Result where D: Deserializer<'de>, { @@ -117,7 +117,13 @@ where } match d.deserialize_string(StringVisitor) { - Ok(value) => Ok(parse_duration(value.as_str())), + Ok(value) => { + return if let Ok(duration) = parse(value.as_str()) { + Ok(duration) + } else { + Ok(Duration::from_secs(60)) + } + } Err(err) => Err(err), } } @@ -141,7 +147,7 @@ mod tests { ) .unwrap(); - assert_eq!(config.interval, Some(Duration::from_secs(123))); + assert_eq!(config.interval, Duration::from_secs(123)); } #[test] @@ -157,7 +163,7 @@ mod tests { ) .unwrap(); - assert_eq!(config.interval, Some(Duration::from_secs(123))); + assert_eq!(config.interval, Duration::from_secs(123)); } #[test] @@ -171,7 +177,7 @@ mod tests { ) .unwrap(); - assert_eq!(config.interval, Some(Duration::from_secs(60))); + assert_eq!(config.interval, Duration::from_secs(60)); } #[test] @@ -203,7 +209,7 @@ mod tests { #[test] fn test_should_read_and_parse_file() { let config = Config::read_file("./tests/testconfig1.toml"); - assert_eq!(config.interval, Some(Duration::from_secs(10))); + assert_eq!(config.interval, Duration::from_secs(10)); assert_eq!(config.checks.len(), 1); assert_eq!(config.checks[0].name, "www"); assert_eq!(config.checks[0].url, "https://example.com"); @@ -212,7 +218,7 @@ mod tests { #[test] fn test_should_return_default_if_no_config_file() { let config = Config::read_file("./tests/no_testconfig.toml"); - assert_eq!(config.interval, Some(Duration::from_secs(60))); + assert_eq!(config.interval, Duration::from_secs(60)); assert_eq!(config.colors.up, "#00FF00".to_string()); assert_eq!(config.colors.warn, "#FFFF00".to_string()); assert_eq!(config.colors.down, "#FF0000".to_string()); diff --git a/src/main.rs b/src/main.rs index 3b0a1f9..92cd640 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use checkbar::{get_click_cmd, print_states, read_click_event, run_click_cmd, Config, MouseButton}; use serde_json::json; -use std::time::Duration; use tokio::task; #[tokio::main(flavor = "multi_thread", worker_threads = 2)] @@ -32,11 +31,7 @@ async fn main() { loop { let config = Config::read(); print_states(&config.checks).await; - let interval = match config.interval { - Some(value) => value, - _ => Duration::from_secs(60), - }; - std::thread::sleep(interval); + std::thread::sleep(config.interval); } });