From 33cb3cdb9d2d8739198e484f4461436fa4d41a93 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Sun, 21 Apr 2024 14:13:30 +0800 Subject: [PATCH] fix #31 --- Cargo.toml | 2 +- src/lib.rs | 3 +- src/parser.rs | 94 ++++++++++++++++++++++++--------------------------- 3 files changed, 48 insertions(+), 51 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e3971fb..ee0656d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "duration-str" -version = "0.8.0" +version = "0.8.1" authors = ["baoyachi "] edition = "2021" description = "duration string parser" diff --git a/src/lib.rs b/src/lib.rs index 1ec3d74..bd4090d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -198,7 +198,7 @@ pub enum DError { OverflowError, } -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Default)] enum TimeUnit { Year, Month, @@ -206,6 +206,7 @@ enum TimeUnit { Day, Hour, Minute, + #[default] Second, MilliSecond, MicroSecond, diff --git a/src/parser.rs b/src/parser.rs index 5458acf..09f6b04 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -18,7 +18,12 @@ fn cond_unit(input: &mut &str) -> PResult { } pub(crate) fn parse_expr_time(input: &mut &str) -> PResult { - (digit1, unit_abbr) + ( + multispace0, + digit1, + opt(unit_abbr).map(Option::unwrap_or_default), + ) + .map(|x| (x.1, x.2)) .try_map(|(v, unit)| unit.duration(v)) .parse_next(input) } @@ -33,7 +38,7 @@ pub(crate) fn cond_time<'a>(input: &mut &'a str) -> PResult