From 73f69b1da0753545bfc16325cd27234ff73a4081 Mon Sep 17 00:00:00 2001 From: yuankunzhang Date: Tue, 17 Jun 2025 22:08:35 +0800 Subject: [PATCH] fix: weekday may be followed by an optional comma --- src/items/weekday.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/items/weekday.rs b/src/items/weekday.rs index 138e14c..7a64749 100644 --- a/src/items/weekday.rs +++ b/src/items/weekday.rs @@ -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 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 { 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, + } + ); + } + } }