From 139cc94bf770fa657980d34448720804ad520d1f Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Mon, 9 Jan 2023 21:03:34 +0100 Subject: [PATCH] Simplified duration parsing --- src/config.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3461655..3f95653 100644 --- a/src/config.rs +++ b/src/config.rs @@ -111,35 +111,26 @@ where } fn parse_duration(value: &str) -> Option { - let mut result = 0; + let mut duration_in_secs = 0; 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).next().unwrap(); if let Some(hours) = parts.name("hours") { - result += match hours.as_str().parse::() { - Ok(value) => value * 60 * 60, - _ => 0, - }; + duration_in_secs += hours.as_str().parse::().unwrap_or(0) * 60 * 60 } if let Some(minutes) = parts.name("minutes") { - result += match minutes.as_str().parse::() { - Ok(value) => value * 60, - _ => 0, - }; + duration_in_secs += minutes.as_str().parse::().unwrap_or(0) * 60 } if let Some(seconds) = parts.name("seconds") { - result += match seconds.as_str().parse::() { - Ok(value) => value, - _ => 0, - }; + duration_in_secs += seconds.as_str().parse::().unwrap_or(0) } } else { return None; } } - Some(Duration::from_secs(result)) + Some(Duration::from_secs(duration_in_secs)) } #[cfg(test)]