1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::{cmp::Ordering, str::FromStr};

use nom::{
    bytes::complete::take,
    character::complete::{char, digit1},
    combinator::{all_consuming, cut, map_res, peek, verify},
    sequence::tuple,
    Finish,
};

use super::{IResult, Span};

/// A date
///
/// The parser has some sanity checks to make sure the date remotely makes sense
/// but it doesn't verify if it is an actual real valid date.
///
/// If that is important to you, you should use a date-time library to verify the validity.
///
/// # Example
///
/// ```
/// # use beancount_parser::BeancountFile;
/// let input = "2022-05-21 event \"location\" \"Middle earth\"";
/// let beancount: BeancountFile<f64> = input.parse().unwrap();
/// let date = beancount.directives[0].date;
/// assert_eq!(date.year, 2022);
/// assert_eq!(date.month, 5);
/// assert_eq!(date.day, 21);
/// ```
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Date {
    /// Year
    pub year: u16,
    /// Month (of year)
    pub month: u8,
    /// Day (of month)
    pub day: u8,
}

impl Date {
    /// Create a new date from year, month and day
    #[must_use]
    pub fn new(year: u16, month: u8, day: u8) -> Self {
        Self { year, month, day }
    }
}

impl PartialOrd for Date {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Date {
    fn cmp(&self, other: &Self) -> Ordering {
        self.year
            .cmp(&other.year)
            .then_with(|| self.month.cmp(&other.month))
            .then_with(|| self.day.cmp(&other.day))
    }
}

impl FromStr for Date {
    type Err = crate::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let span = Span::new(s);
        match all_consuming(parse)(span).finish() {
            Ok((_, date)) => Ok(date),
            Err(_) => Err(crate::Error::new(s, span)),
        }
    }
}

pub(super) fn parse(input: Span<'_>) -> IResult<'_, Date> {
    let (input, _) = peek(tuple((digit1, char('-'), digit1, char('-'), digit1)))(input)?;
    cut(do_parse)(input)
}

fn do_parse(input: Span<'_>) -> IResult<'_, Date> {
    let (input, year) = year(input)?;
    let (input, _) = char('-')(input)?;
    let (input, month) = month(input)?;
    let (input, _) = char('-')(input)?;
    let (input, day) = day(input)?;
    Ok((input, Date { year, month, day }))
}

fn year(input: Span<'_>) -> IResult<'_, u16> {
    map_res(take(4usize), |s: Span<'_>| s.fragment().parse())(input)
}

fn month(input: Span<'_>) -> IResult<'_, u8> {
    verify(
        map_res(take(2usize), |s: Span<'_>| s.fragment().parse()),
        |&n| n > 0 && n < 13,
    )(input)
}

fn day(input: Span<'_>) -> IResult<'_, u8> {
    verify(
        map_res(take(2usize), |s: Span<'_>| s.fragment().parse()),
        |&n| n > 0 && n < 32,
    )(input)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn date_from_str_should_parse_valid_date() {
        let date: Date = "2023-03-12".parse().unwrap();
        assert_eq!(date.year, 2023);
        assert_eq!(date.month, 3);
        assert_eq!(date.day, 12);
    }

    #[test]
    fn date_from_str_should_not_parse_invalid_date() {
        let result: Result<Date, _> = "2023-03-12oops".parse();
        assert!(result.is_err(), "{result:?}");
    }
}

#[cfg(test)]
pub(crate) mod chumsky {
    use crate::{ChumskyError, ChumskyParser, Date};

    use chumsky::prelude::*;

    pub(crate) fn date() -> impl ChumskyParser<Date> {
        year()
            .then_ignore(just('-'))
            .then(month())
            .then_ignore(just('-'))
            .then(day())
            .map(|((year, month), day)| Date::new(year, month, day))
            .labelled("date")
    }

    fn year() -> impl ChumskyParser<u16> {
        filter(|c: &char| c.is_ascii_digit())
            .repeated()
            .exactly(4)
            .collect::<String>()
            .from_str()
            .unwrapped()
            .labelled("year")
    }

    fn month() -> impl ChumskyParser<u8> {
        filter(|c: &char| c.is_ascii_digit())
            .repeated()
            .exactly(2)
            .collect::<String>()
            .from_str()
            .unwrapped()
            .validate(|m: u8, span, emit| {
                if m == 0 || m > 12 {
                    emit(ChumskyError::custom(span, "must be between 1 and 12"));
                }
                m
            })
            .labelled("month")
    }

    fn day() -> impl ChumskyParser<u8> {
        filter(|c: &char| c.is_ascii_digit())
            .repeated()
            .exactly(2)
            .collect::<String>()
            .from_str()
            .unwrapped()
            .validate(|d: u8, span, emit| {
                if d == 0 || d > 31 {
                    emit(ChumskyError::custom(span, "must be between 1 and 31"));
                }
                d
            })
            .labelled("day")
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use rstest::rstest;

        #[rstest]
        #[case::first_day_of_year("2023-01-01", Date::new(2023, 1, 1))]
        #[case::last_day_of_year("2023-12-31", Date::new(2023, 12, 31))]
        fn should_parse_valid_date(#[case] input: &str, #[case] expected: Date) {
            let date: Date = date().then_ignore(end()).parse(input).unwrap();
            assert_eq!(date, expected);
        }

        #[rstest]
        #[case("2023-13-01")]
        #[case("2023-10-32")]
        #[case("2023-01-00")]
        #[case("2023-00-01")]
        #[case("2023-1-2")]
        #[case("2023-01-2")]
        #[case("2023-1-02")]
        #[case("23-01-02")]
        fn should_not_parse_invalid_date(#[case] input: &str) {
            let result: Result<Date, _> = date().then_ignore(end()).parse(input);
            assert!(result.is_err(), "{result:?}");
        }
    }
}