Debian Codegraph @

debian/rust-vendor/parse_datetime/src/items/weekday.rs

raw
1 // For the full copyright and license information, please view the LICENSE
2 // file that was distributed with this source code.
3
4 // spell-checker:ignore wednes
5
6 //! The GNU docs state:
7 //!
8 //! > The explicit mention of a day of the week will forward the date (only if
9 //! > necessary) to reach that day of the week in the future.
10 //! >
11 //! > Days of the week may be spelled out in full: ‘Sunday’, ‘Monday’,
12 //! > ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’ or ‘Saturday’. Days may be
13 //! > abbreviated to their first three letters, optionally followed by a
14 //! > period. The special abbreviations ‘Tues’ for ‘Tuesday’, ‘Wednes’ for
15 //! > ‘Wednesday’ and ‘Thur’ or ‘Thurs’ for ‘Thursday’ are also allowed.
16 //! >
17 //! > A number may precede a day of the week item to move forward supplementary
18 //! > weeks. It is best used in expression like ‘third monday’. In this
19 //! > context, ‘last day’ or ‘next day’ is also acceptable; they move one week
20 //! > before or after the day that day by itself would represent.
21 //! >
22 //! > A comma following a day of the week item is ignored.
23
24 use winnow::{
25 ascii::alpha1,
26 combinator::{opt, terminated},
27 seq, ModalResult, Parser,
28 };
29
30 use super::{ordinal::ordinal, primitive::s};
31
32 #[derive(PartialEq, Eq, Debug, Copy, Clone)]
33 pub(crate) enum Day {
34 Monday,
35 Tuesday,
36 Wednesday,
37 Thursday,
38 Friday,
39 Saturday,
40 Sunday,
41 }
42
43 #[derive(PartialEq, Eq, Debug, Clone)]
44 pub(crate) struct Weekday {
45 pub(crate) offset: i32,
46 pub(crate) day: Day,
47 }
48
49 impl From<Day> for jiff::civil::Weekday {
50 fn from(value: Day) -> Self {
51 match value {
52 Day::Monday => jiff::civil::Weekday::Monday,
53 Day::Tuesday => jiff::civil::Weekday::Tuesday,
54 Day::Wednesday => jiff::civil::Weekday::Wednesday,
55 Day::Thursday => jiff::civil::Weekday::Thursday,
56 Day::Friday => jiff::civil::Weekday::Friday,
57 Day::Saturday => jiff::civil::Weekday::Saturday,
58 Day::Sunday => jiff::civil::Weekday::Sunday,
59 }
60 }
61 }
62
63 /// Parse a weekday item.
64 pub(super) fn parse(input: &mut &str) -> ModalResult<Weekday> {
65 seq!(Weekday {
66 offset: opt(ordinal).map(|o| o.unwrap_or_default()),
67 day: terminated(day, opt(s(","))),
68 })
69 .parse_next(input)
70 }
71
72 fn day(input: &mut &str) -> ModalResult<Day> {
73 s(alpha1)
74 .verify_map(|s: &str| {
75 Some(match s {
76 "monday" | "mon" | "mon." => Day::Monday,
77 "tuesday" | "tue" | "tue." | "tues" => Day::Tuesday,
78 "wednesday" | "wed" | "wed." | "wednes" => Day::Wednesday,
79 "thursday" | "thu" | "thu." | "thur" | "thurs" => Day::Thursday,
80 "friday" | "fri" | "fri." => Day::Friday,
81 "saturday" | "sat" | "sat." => Day::Saturday,
82 "sunday" | "sun" | "sun." => Day::Sunday,
83 _ => return None,
84 })
85 })
86 .parse_next(input)
87 }
88
89 #[cfg(test)]
90 mod tests {
91 use super::{parse, Day, Weekday};
92
93 #[test]
94 fn this_monday() {
95 for mut s in [
96 "monday",
97 "mon",
98 "mon.",
99 "this monday",
100 "this mon",
101 "this mon.",
102 "this monday",
103 "this - monday",
104 "0 monday",
105 ] {
106 assert_eq!(
107 parse(&mut s).unwrap(),
108 Weekday {
109 offset: 0,
110 day: Day::Monday,
111 }
112 );
113 }
114 }
115
116 #[test]
117 fn next_tuesday() {
118 for s in ["tuesday", "tue", "tue.", "tues"] {
119 let s = format!("next {s}");
120 assert_eq!(
121 parse(&mut s.as_ref()).unwrap(),
122 Weekday {
123 offset: 1,
124 day: Day::Tuesday,
125 }
126 );
127 }
128 }
129
130 #[test]
131 fn last_wednesday() {
132 for s in ["wednesday", "wed", "wed.", "wednes"] {
133 let s = format!("last {s}");
134 assert_eq!(
135 parse(&mut s.as_ref()).unwrap(),
136 Weekday {
137 offset: -1,
138 day: Day::Wednesday,
139 }
140 );
141 }
142 }
143
144 #[test]
145 fn optional_comma() {
146 for mut s in ["monday,", "mon,", "mon.,", "mon. ,"] {
147 assert_eq!(
148 parse(&mut s).unwrap(),
149 Weekday {
150 offset: 0,
151 day: Day::Monday,
152 }
153 );
154 }
155 }
156 }