Fix try_parse_duration fn
This commit is contained in:
@@ -22,28 +22,33 @@ lazy_static! {
|
|||||||
|
|
||||||
// Parse time format into Duration
|
// Parse time format into Duration
|
||||||
pub fn try_parse_duration(text: &str) -> result::Result<Duration, ()> {
|
pub fn try_parse_duration(text: &str) -> result::Result<Duration, ()> {
|
||||||
lazy_static! {
|
let numbers = "0123456789.".chars().collect::<Vec<char>>();
|
||||||
static ref REGEX_MATCH_TIME: Regex =
|
let i = text
|
||||||
Regex::new(r"^(?P<time>\d+(\.\d+)?)(?P<unit>d|h|m|s|ms)$").unwrap();
|
.chars()
|
||||||
|
.position(|ch| !numbers.contains(&ch))
|
||||||
|
.ok_or_else(|| ())?;
|
||||||
|
|
||||||
|
let time = &text[..i];
|
||||||
|
let unit = &text[i..];
|
||||||
|
|
||||||
|
if time.is_empty() {
|
||||||
|
return Err(());
|
||||||
}
|
}
|
||||||
let reg: &Regex = ®EX_MATCH_TIME;
|
let n = time.parse::<f64>().map_err(|_| ())?;
|
||||||
|
let ms = match unit {
|
||||||
|
"d" => Ok(24_f64 * 60_f64 * 60_f64 * 1000_f64 * n),
|
||||||
|
"h" => Ok(60_f64 * 60_f64 * 1000_f64 * n),
|
||||||
|
"m" => Ok(60_f64 * 1000_f64 * n),
|
||||||
|
"s" => Ok(1000_f64 * n),
|
||||||
|
"ms" => Ok(n),
|
||||||
|
_ => Err(()),
|
||||||
|
}? as u64;
|
||||||
|
|
||||||
let cap = reg.captures(text).ok_or_else(|| ())?;
|
if ms == 0 {
|
||||||
let time = cap.name("time").unwrap();
|
Err(())
|
||||||
let unit = cap.name("unit").unwrap();
|
} else {
|
||||||
|
Ok(Duration::from_millis(ms))
|
||||||
let n = time.as_str().parse::<f64>().map_err(|_| ())?;
|
}
|
||||||
|
|
||||||
let ms = match unit.as_str() {
|
|
||||||
"d" => 24_f64 * 60_f64 * 60_f64 * 1000_f64 * n,
|
|
||||||
"h" => 60_f64 * 60_f64 * 1000_f64 * n,
|
|
||||||
"m" => 60_f64 * 1000_f64 * n,
|
|
||||||
"s" => 1000_f64 * n,
|
|
||||||
"ms" => n,
|
|
||||||
_ => panic!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Duration::from_millis(ms as u64))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
Reference in New Issue
Block a user