mirror of
				https://github.com/pcvolkmer/checkbar.git
				synced 2025-10-31 03:46:12 +00:00 
			
		
		
		
	Adding long unit forms for interval duration
This commit is contained in:
		| @@ -31,8 +31,13 @@ url = "tcp://app.example.com:12345" | |||||||
| check_type = "Tcp" | check_type = "Tcp" | ||||||
| ---- | ---- | ||||||
|  |  | ||||||
| The value for `interval` can be set by using plain seconds or using units of `h`, `m` and `s`. Unparseable values will | The value for `interval` can be set by using plain seconds or using units. Accepted units: | ||||||
| default to 60 seconds. |  | ||||||
|  | * h, hour, hours | ||||||
|  | * m, min, mins, minutes, minutes | ||||||
|  | * s, sec, secs, second, seconds | ||||||
|  |  | ||||||
|  | Unparseable values will default to 60 seconds. | ||||||
|  |  | ||||||
| ---- | ---- | ||||||
| # Update interval using seconds as number. | # Update interval using seconds as number. | ||||||
|   | |||||||
| @@ -107,9 +107,9 @@ where | |||||||
|  |  | ||||||
| fn parse_duration(value: &str) -> Option<Duration> { | fn parse_duration(value: &str) -> Option<Duration> { | ||||||
|     let mut duration_in_secs = 0; |     let mut duration_in_secs = 0; | ||||||
|     if let Ok(re) = |     if let Ok(re) = Regex::new( | ||||||
|         Regex::new(r"^((?P<hours>\d+)h\s*)?((?P<minutes>\d+)m\s*)?((?P<seconds>\d+)s?\s*)?$") |         r"^((?P<hours>\d+)(h|hour|hours)\s*)?((?P<minutes>\d+)(m|min|mins|minute|minutes)\s*)?((?P<seconds>\d+)(s|sec|secs|second|seconds)?\s*)?$", | ||||||
|     { |     ) { | ||||||
|         if re.is_match(value) { |         if re.is_match(value) { | ||||||
|             let parts = re.captures_iter(value).next().unwrap(); |             let parts = re.captures_iter(value).next().unwrap(); | ||||||
|             if let Some(hours) = parts.name("hours") { |             if let Some(hours) = parts.name("hours") { | ||||||
| @@ -222,18 +222,51 @@ mod tests { | |||||||
|         assert_eq!(config.checks.len(), 0); |         assert_eq!(config.checks.len(), 0); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     macro_rules! multi_assert_eq { | ||||||
|  |         ($f:expr, $expected:expr, $ ( $left:expr ), + ) => { | ||||||
|  |             $( | ||||||
|  |                  assert_eq!($f($left), $expected, $left); | ||||||
|  |             )+ | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_should_parse_durations() { |     fn test_should_parse_durations() { | ||||||
|         assert_eq!(parse_duration("1m30s"), Some(Duration::from_secs(90))); |         assert_eq!(parse_duration("1m30s"), Some(Duration::from_secs(90))); | ||||||
|         assert_eq!(parse_duration("2m"), Some(Duration::from_secs(120))); |         assert_eq!(parse_duration("2m"), Some(Duration::from_secs(120))); | ||||||
|         assert_eq!(parse_duration("1h1m1s"), Some(Duration::from_secs(3661))); |  | ||||||
|         assert_eq!(parse_duration("90"), Some(Duration::from_secs(90))); |         assert_eq!(parse_duration("90"), Some(Duration::from_secs(90))); | ||||||
|  |  | ||||||
|  |         multi_assert_eq!( | ||||||
|  |             parse_duration, | ||||||
|  |             Some(Duration::from_secs(3661)), | ||||||
|  |             "1h1m1s", | ||||||
|  |             "1hour1min1s", | ||||||
|  |             "1hour1mins1s", | ||||||
|  |             "1hour1minute1sec", | ||||||
|  |             "1hour1minute1secs", | ||||||
|  |             "1hour1minutes1second", | ||||||
|  |             "1hour1minutes1seconds", | ||||||
|  |             "1hours1minutes1seconds" | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_should_parse_durations_with_whitespaces() { |     fn test_should_parse_durations_with_whitespaces() { | ||||||
|         assert_eq!(parse_duration("1m 30s"), Some(Duration::from_secs(90))); |         assert_eq!(parse_duration("1m 30s"), Some(Duration::from_secs(90))); | ||||||
|         assert_eq!(parse_duration("1h 1m 1s"), Some(Duration::from_secs(3661))); |         assert_eq!(parse_duration("1h 1m 1s"), Some(Duration::from_secs(3661))); | ||||||
|  |  | ||||||
|  |         multi_assert_eq!( | ||||||
|  |             parse_duration, | ||||||
|  |             Some(Duration::from_secs(3661)), | ||||||
|  |             "1h 1m 1s", | ||||||
|  |             "1hour 1min 1s", | ||||||
|  |             "1hour 1mins 1s", | ||||||
|  |             "1hour 1minute 1sec", | ||||||
|  |             "1hour 1minute 1secs", | ||||||
|  |             "1hour 1minutes 1second", | ||||||
|  |             "1hour 1minutes 1seconds", | ||||||
|  |             "1hours 1minutes 1seconds" | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user