Skip to content

Commit 9589746

Browse files
authored
Merge pull request #159 from yuankunzhang/weekday-optional-comma
fix: weekday may be followed by an optional comma
2 parents 30cb26d + 73f69b1 commit 9589746

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/items/weekday.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
//! >
2222
//! > A comma following a day of the week item is ignored.
2323
24-
use winnow::{ascii::alpha1, combinator::opt, seq, ModalResult, Parser};
24+
use winnow::{
25+
ascii::alpha1,
26+
combinator::{opt, terminated},
27+
seq, ModalResult, Parser,
28+
};
2529

2630
use super::{ordinal::ordinal, primitive::s};
2731

@@ -55,10 +59,26 @@ impl From<Day> for chrono::Weekday {
5559
}
5660
}
5761
}
62+
63+
/// Parse a weekday item.
64+
///
65+
/// Grammar:
66+
///
67+
/// ```ebnf
68+
/// weekday = [ ordinal ] day [ "," ] ;
69+
///
70+
/// day = "monday" | "mon" | "mon."
71+
/// | "tuesday" | "tue" | "tue." | "tues"
72+
/// | "wednesday" | "wed" | "wed." | "wednes"
73+
/// | "thursday" | "thu" | "thu." | "thur" | "thurs"
74+
/// | "friday" | "fri" | "fri."
75+
/// | "saturday" | "sat" | "sat."
76+
/// | "sunday" | "sun" | "sun." ;
77+
/// ```
5878
pub fn parse(input: &mut &str) -> ModalResult<Weekday> {
5979
seq!(Weekday {
6080
offset: opt(ordinal).map(|o| o.unwrap_or_default()),
61-
day: day,
81+
day: terminated(day, opt(s(","))),
6282
})
6383
.parse_next(input)
6484
}
@@ -134,4 +154,17 @@ mod tests {
134154
);
135155
}
136156
}
157+
158+
#[test]
159+
fn optional_comma() {
160+
for mut s in ["monday,", "mon,", "mon.,", "mon. ,"] {
161+
assert_eq!(
162+
parse(&mut s).unwrap(),
163+
Weekday {
164+
offset: 0,
165+
day: Day::Monday,
166+
}
167+
);
168+
}
169+
}
137170
}

0 commit comments

Comments
 (0)