1use std::{cmp::Ordering, str::FromStr};
2
3use nom::{
4 bytes::complete::take,
5 character::complete::{char, digit1},
6 combinator::{all_consuming, cut, map_res, peek, verify},
7 sequence::tuple,
8 Finish,
9};
10
11use super::{IResult, Span};
12
13#[derive(Debug, Copy, Clone, Eq, PartialEq)]
32pub struct Date {
33 pub year: u16,
35 pub month: u8,
37 pub day: u8,
39}
40
41impl Date {
42 #[must_use]
44 pub fn new(year: u16, month: u8, day: u8) -> Self {
45 Self { year, month, day }
46 }
47}
48
49impl PartialOrd for Date {
50 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
51 Some(self.cmp(other))
52 }
53}
54
55impl Ord for Date {
56 fn cmp(&self, other: &Self) -> Ordering {
57 self.year
58 .cmp(&other.year)
59 .then_with(|| self.month.cmp(&other.month))
60 .then_with(|| self.day.cmp(&other.day))
61 }
62}
63
64impl FromStr for Date {
65 type Err = crate::Error;
66
67 fn from_str(s: &str) -> Result<Self, Self::Err> {
68 let span = Span::new(s);
69 match all_consuming(parse)(span).finish() {
70 Ok((_, date)) => Ok(date),
71 Err(_) => Err(crate::Error::new(s, span)),
72 }
73 }
74}
75
76pub(super) fn parse(input: Span<'_>) -> IResult<'_, Date> {
77 let (input, _) = peek(tuple((digit1, char('-'), digit1, char('-'), digit1)))(input)?;
78 cut(do_parse)(input)
79}
80
81fn do_parse(input: Span<'_>) -> IResult<'_, Date> {
82 let (input, year) = year(input)?;
83 let (input, _) = char('-')(input)?;
84 let (input, month) = month(input)?;
85 let (input, _) = char('-')(input)?;
86 let (input, day) = day(input)?;
87 Ok((input, Date { year, month, day }))
88}
89
90fn year(input: Span<'_>) -> IResult<'_, u16> {
91 map_res(take(4usize), |s: Span<'_>| s.fragment().parse())(input)
92}
93
94fn month(input: Span<'_>) -> IResult<'_, u8> {
95 verify(
96 map_res(take(2usize), |s: Span<'_>| s.fragment().parse()),
97 |&n| n > 0 && n < 13,
98 )(input)
99}
100
101fn day(input: Span<'_>) -> IResult<'_, u8> {
102 verify(
103 map_res(take(2usize), |s: Span<'_>| s.fragment().parse()),
104 |&n| n > 0 && n < 32,
105 )(input)
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn date_from_str_should_parse_valid_date() {
114 let date: Date = "2023-03-12".parse().unwrap();
115 assert_eq!(date.year, 2023);
116 assert_eq!(date.month, 3);
117 assert_eq!(date.day, 12);
118 }
119
120 #[test]
121 fn date_from_str_should_not_parse_invalid_date() {
122 let result: Result<Date, _> = "2023-03-12oops".parse();
123 assert!(result.is_err(), "{result:?}");
124 }
125}
126
127#[cfg(test)]
128pub(crate) mod chumsky {
129 use crate::{ChumskyError, ChumskyParser, Date};
130
131 use chumsky::prelude::*;
132
133 pub(crate) fn date() -> impl ChumskyParser<Date> {
134 year()
135 .then_ignore(just('-'))
136 .then(month())
137 .then_ignore(just('-'))
138 .then(day())
139 .map(|((year, month), day)| Date::new(year, month, day))
140 .labelled("date")
141 }
142
143 fn year() -> impl ChumskyParser<u16> {
144 filter(|c: &char| c.is_ascii_digit())
145 .repeated()
146 .exactly(4)
147 .collect::<String>()
148 .from_str()
149 .unwrapped()
150 .labelled("year")
151 }
152
153 fn month() -> impl ChumskyParser<u8> {
154 filter(|c: &char| c.is_ascii_digit())
155 .repeated()
156 .exactly(2)
157 .collect::<String>()
158 .from_str()
159 .unwrapped()
160 .validate(|m: u8, span, emit| {
161 if m == 0 || m > 12 {
162 emit(ChumskyError::custom(span, "must be between 1 and 12"));
163 }
164 m
165 })
166 .labelled("month")
167 }
168
169 fn day() -> impl ChumskyParser<u8> {
170 filter(|c: &char| c.is_ascii_digit())
171 .repeated()
172 .exactly(2)
173 .collect::<String>()
174 .from_str()
175 .unwrapped()
176 .validate(|d: u8, span, emit| {
177 if d == 0 || d > 31 {
178 emit(ChumskyError::custom(span, "must be between 1 and 31"));
179 }
180 d
181 })
182 .labelled("day")
183 }
184
185 #[cfg(test)]
186 mod tests {
187 use super::*;
188 use rstest::rstest;
189
190 #[rstest]
191 #[case::first_day_of_year("2023-01-01", Date::new(2023, 1, 1))]
192 #[case::last_day_of_year("2023-12-31", Date::new(2023, 12, 31))]
193 fn should_parse_valid_date(#[case] input: &str, #[case] expected: Date) {
194 let date: Date = date().then_ignore(end()).parse(input).unwrap();
195 assert_eq!(date, expected);
196 }
197
198 #[rstest]
199 #[case("2023-13-01")]
200 #[case("2023-10-32")]
201 #[case("2023-01-00")]
202 #[case("2023-00-01")]
203 #[case("2023-1-2")]
204 #[case("2023-01-2")]
205 #[case("2023-1-02")]
206 #[case("23-01-02")]
207 fn should_not_parse_invalid_date(#[case] input: &str) {
208 let result: Result<Date, _> = date().then_ignore(end()).parse(input);
209 assert!(result.is_err(), "{result:?}");
210 }
211 }
212}