diff --git a/README.adoc b/README.adoc index f9d13d4..a41992f 100644 --- a/README.adoc +++ b/README.adoc @@ -36,7 +36,7 @@ default to 60 seconds. ---- # Update interval using value with units. Default value if not set is 60 sec. -interval = 2m30s +interval = "2m 30s" ---- Each host or application to be checked consists of `name` and `url`. @@ -58,7 +58,7 @@ $ checkbar /etc/checkbar_example.toml === Colors -To change the colors, use the following configuration. As an example the colors of the default configuration are shown. +To change the colors, use the following configuration. As an example, the colors of the default configuration are shown. ---- ... diff --git a/src/config.rs b/src/config.rs index 71efd47..0e7831b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -69,23 +69,25 @@ pub fn parse_duration(value: Option) -> Duration { } _ => return Duration::from_secs(60), }; - if let Ok(re) = Regex::new(r"^((?P\d+)h)?((?P\d+)m)?((?P\d+)s?)?$") { + if let Ok(re) = + Regex::new(r"^((?P\d+)h\s*)?((?P\d+)m\s*)?((?P\d+)s?\s*)?$") + { if re.is_match(value) { - let parts = re.captures_iter(value).nth(0).unwrap(); + let parts = re.captures_iter(value).next().unwrap(); if let Some(hours) = parts.name("hours") { - result += match u64::from_str_radix(hours.as_str(), 10) { + result += match hours.as_str().parse::() { Ok(value) => value * 60 * 60, _ => 0, }; } if let Some(minutes) = parts.name("minutes") { - result += match u64::from_str_radix(minutes.as_str(), 10) { + result += match minutes.as_str().parse::() { Ok(value) => value * 60, _ => 0, }; } if let Some(seconds) = parts.name("seconds") { - result += match u64::from_str_radix(seconds.as_str(), 10) { + result += match seconds.as_str().parse::() { Ok(value) => value, _ => 0, }; @@ -122,6 +124,18 @@ mod tests { ); } + #[test] + fn test_should_parse_durations_with_whitespaces() { + assert_eq!( + parse_duration(Some("1m 30s".to_string())), + Duration::from_secs(90) + ); + assert_eq!( + parse_duration(Some("1h 1m 1s".to_string())), + Duration::from_secs(3661) + ); + } + #[test] fn test_should_return_default_for_unparseable_durations() { assert_eq!(parse_duration(None), Duration::from_secs(60));