You've already forked parse_datetime
mirror of
https://github.com/uutils/parse_datetime.git
synced 2026-06-10 16:13:15 -07:00
fix: weekday may be followed by an optional comma
This commit is contained in:
+35
-2
@@ -21,7 +21,11 @@
|
||||
//! >
|
||||
//! > A comma following a day of the week item is ignored.
|
||||
|
||||
use winnow::{ascii::alpha1, combinator::opt, seq, ModalResult, Parser};
|
||||
use winnow::{
|
||||
ascii::alpha1,
|
||||
combinator::{opt, terminated},
|
||||
seq, ModalResult, Parser,
|
||||
};
|
||||
|
||||
use super::{ordinal::ordinal, primitive::s};
|
||||
|
||||
@@ -55,10 +59,26 @@ impl From<Day> for chrono::Weekday {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a weekday item.
|
||||
///
|
||||
/// Grammar:
|
||||
///
|
||||
/// ```ebnf
|
||||
/// weekday = [ ordinal ] day [ "," ] ;
|
||||
///
|
||||
/// day = "monday" | "mon" | "mon."
|
||||
/// | "tuesday" | "tue" | "tue." | "tues"
|
||||
/// | "wednesday" | "wed" | "wed." | "wednes"
|
||||
/// | "thursday" | "thu" | "thu." | "thur" | "thurs"
|
||||
/// | "friday" | "fri" | "fri."
|
||||
/// | "saturday" | "sat" | "sat."
|
||||
/// | "sunday" | "sun" | "sun." ;
|
||||
/// ```
|
||||
pub fn parse(input: &mut &str) -> ModalResult<Weekday> {
|
||||
seq!(Weekday {
|
||||
offset: opt(ordinal).map(|o| o.unwrap_or_default()),
|
||||
day: day,
|
||||
day: terminated(day, opt(s(","))),
|
||||
})
|
||||
.parse_next(input)
|
||||
}
|
||||
@@ -134,4 +154,17 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optional_comma() {
|
||||
for mut s in ["monday,", "mon,", "mon.,", "mon. ,"] {
|
||||
assert_eq!(
|
||||
parse(&mut s).unwrap(),
|
||||
Weekday {
|
||||
offset: 0,
|
||||
day: Day::Monday,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user