hledger_parser/component/period/
interval.rs

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use chumsky::prelude::*;

use crate::{component::whitespace::whitespace, state::State};

#[derive(Debug, Clone, PartialEq)]
pub enum Interval {
    // Every N days
    NthDay(u32),
    // Every N Weeks
    NthWeek(u32),
    // Every N quarters
    NthQuarter(u32),
    // Every N months
    NthMonth(u32),
    // Every N years
    NthYear(u32),
    // Weekly on a week day
    Weekday(chrono::Weekday),
}

// TODO:
// every Nth day [of month] (31st day will be adjusted to each month's last day)
// every Nth WEEKDAYNAME [of month]
// Yearly on a custom month and day:
//
// every MM/DD [of year] (month number and day of month number)
// every MONTHNAME DDth [of year] (full or three-letter english month name, case insensitive, and day of month number)
// every DDth MONTHNAME [of year] (equivalent to the above)
pub fn interval<'a>() -> impl Parser<'a, &'a str, Interval, extra::Full<Rich<'a, char>, State, ()>>
{
    let word = choice([
        just("daily").to(Interval::NthDay(1)),
        just("weekly").to(Interval::NthWeek(1)),
        just("biweekly").to(Interval::NthWeek(2)),
        just("fortnightly").to(Interval::NthWeek(2)),
        just("monthly").to(Interval::NthMonth(1)),
        just("bimonthly").to(Interval::NthMonth(2)),
        just("quarterly").to(Interval::NthQuarter(1)),
        just("yearly").to(Interval::NthQuarter(1)),
    ]);

    word.or(every()).or(day_of_week())
}

fn day_of_week<'a>() -> impl Parser<'a, &'a str, Interval, extra::Full<Rich<'a, char>, State, ()>> {
    let monday = just("monday")
        .ignored()
        .or(just("mon").ignored())
        .or(just("1st")
            .then(whitespace().repeated().at_least(1))
            .then(just("day"))
            .then(whitespace().repeated().at_least(1))
            .then(just("of"))
            .then(whitespace().repeated().at_least(1))
            .then(just("week"))
            .ignored())
        .map(|()| Interval::Weekday(chrono::Weekday::Mon));
    let tuesday = just("tuesday")
        .ignored()
        .or(just("tue").ignored())
        .or(just("2nd")
            .then(whitespace().repeated().at_least(1))
            .then(just("day"))
            .then(whitespace().repeated().at_least(1))
            .then(just("of"))
            .then(whitespace().repeated().at_least(1))
            .then(just("week"))
            .ignored())
        .map(|()| Interval::Weekday(chrono::Weekday::Tue));
    let wednesday = just("wednesday")
        .ignored()
        .or(just("wed").ignored())
        .or(just("3rd")
            .then(whitespace().repeated().at_least(1))
            .then(just("day"))
            .then(whitespace().repeated().at_least(1))
            .then(just("of"))
            .then(whitespace().repeated().at_least(1))
            .then(just("week"))
            .ignored())
        .map(|()| Interval::Weekday(chrono::Weekday::Wed));
    let thursday = just("thursday")
        .ignored()
        .or(just("thu").ignored())
        .or(just("3rd")
            .then(whitespace().repeated().at_least(1))
            .then(just("day"))
            .then(whitespace().repeated().at_least(1))
            .then(just("of"))
            .then(whitespace().repeated().at_least(1))
            .then(just("week"))
            .ignored())
        .map(|()| Interval::Weekday(chrono::Weekday::Thu));
    let friday = just("friday")
        .ignored()
        .or(just("fri").ignored())
        .or(just("3rd")
            .then(whitespace().repeated().at_least(1))
            .then(just("day"))
            .then(whitespace().repeated().at_least(1))
            .then(just("of"))
            .then(whitespace().repeated().at_least(1))
            .then(just("week"))
            .ignored())
        .map(|()| Interval::Weekday(chrono::Weekday::Fri));
    let saturday = just("saturday")
        .ignored()
        .or(just("sat").ignored())
        .or(just("3rd")
            .then(whitespace().repeated().at_least(1))
            .then(just("day"))
            .then(whitespace().repeated().at_least(1))
            .then(just("of"))
            .then(whitespace().repeated().at_least(1))
            .then(just("week"))
            .ignored())
        .map(|()| Interval::Weekday(chrono::Weekday::Sat));
    let sunday = just("sunday")
        .ignored()
        .or(just("sun").ignored())
        .or(just("3rd")
            .then(whitespace().repeated().at_least(1))
            .then(just("day"))
            .then(whitespace().repeated().at_least(1))
            .then(just("of"))
            .then(whitespace().repeated().at_least(1))
            .then(just("week"))
            .ignored())
        .map(|()| Interval::Weekday(chrono::Weekday::Sun));
    just("every")
        .then(whitespace().repeated().at_least(1))
        .ignore_then(
            monday
                .or(tuesday)
                .or(wednesday)
                .or(thursday)
                .or(friday)
                .or(saturday)
                .or(sunday),
        )
}

fn every<'a>() -> impl Parser<'a, &'a str, Interval, extra::Full<Rich<'a, char>, State, ()>> {
    let every = just("every")
        .then(whitespace().repeated().at_least(1))
        .ignore_then(choice([
            just("day").to(Interval::NthDay(1)),
            just("week").to(Interval::NthWeek(1)),
            just("month").to(Interval::NthMonth(1)),
            just("quarter").to(Interval::NthQuarter(1)),
            just("year").to(Interval::NthYear(1)),
        ]));
    let every_n_days = just("every")
        .then(whitespace().repeated().at_least(1))
        .ignore_then(text::int(10).from_str::<u32>().unwrapped())
        .then_ignore(whitespace().repeated().at_least(1))
        .then_ignore(just("days"))
        .map(Interval::NthDay);
    let every_n_weeks = just("every")
        .then(whitespace().repeated().at_least(1))
        .ignore_then(text::int(10).from_str::<u32>().unwrapped())
        .then_ignore(whitespace().repeated().at_least(1))
        .then_ignore(just("weeks"))
        .map(Interval::NthWeek);
    let every_n_months = just("every")
        .then(whitespace().repeated().at_least(1))
        .ignore_then(text::int(10).from_str::<u32>().unwrapped())
        .then_ignore(whitespace().repeated().at_least(1))
        .then_ignore(just("months"))
        .map(Interval::NthMonth);
    let every_n_quarterd = just("every")
        .then(whitespace().repeated().at_least(1))
        .ignore_then(text::int(10).from_str::<u32>().unwrapped())
        .then_ignore(whitespace().repeated().at_least(1))
        .then_ignore(just("quarters"))
        .map(Interval::NthQuarter);
    let every_n_years = just("every")
        .then(whitespace().repeated().at_least(1))
        .ignore_then(text::int(10).from_str::<u32>().unwrapped())
        .then_ignore(whitespace().repeated().at_least(1))
        .then_ignore(just("years"))
        .map(Interval::NthYear);
    let every_n = every_n_days
        .or(every_n_weeks)
        .or(every_n_months)
        .or(every_n_quarterd)
        .or(every_n_years);
    every.or(every_n)
}

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

    #[test]
    fn every_day() {
        let result = interval()
            .then_ignore(end())
            .parse("every  day")
            .into_result();
        assert_eq!(result, Ok(Interval::NthDay(1)));
    }

    #[test]
    fn every_n_day() {
        let result = interval()
            .then_ignore(end())
            .parse("every 3 days")
            .into_result();
        assert_eq!(result, Ok(Interval::NthDay(3)));
    }

    #[test]
    fn every_week() {
        let result = interval()
            .then_ignore(end())
            .parse("every week")
            .into_result();
        assert_eq!(result, Ok(Interval::NthWeek(1)));
    }

    #[test]
    fn every_n_week() {
        let result = interval()
            .then_ignore(end())
            .parse("every 4 weeks")
            .into_result();
        assert_eq!(result, Ok(Interval::NthWeek(4)));
    }

    #[test]
    fn every_month() {
        let result = interval()
            .then_ignore(end())
            .parse("every month")
            .into_result();
        assert_eq!(result, Ok(Interval::NthMonth(1)));
    }

    #[test]
    fn every_n_month() {
        let result = interval()
            .then_ignore(end())
            .parse("every 2 months")
            .into_result();
        assert_eq!(result, Ok(Interval::NthMonth(2)));
    }

    #[test]
    fn every_quarter() {
        let result = interval()
            .then_ignore(end())
            .parse("every quarter")
            .into_result();
        assert_eq!(result, Ok(Interval::NthQuarter(1)));
    }

    #[test]
    fn every_n_quarter() {
        let result = interval()
            .then_ignore(end())
            .parse("every 2 quarters")
            .into_result();
        assert_eq!(result, Ok(Interval::NthQuarter(2)));
    }

    #[test]
    fn every_year() {
        let result = interval()
            .then_ignore(end())
            .parse("every year")
            .into_result();
        assert_eq!(result, Ok(Interval::NthYear(1)));
    }

    #[test]
    fn every_n_years() {
        let result = interval()
            .then_ignore(end())
            .parse("every 10 years")
            .into_result();
        assert_eq!(result, Ok(Interval::NthYear(10)));
    }

    #[test]
    fn every_weekday() {
        let result = interval()
            .then_ignore(end())
            .parse("every tue")
            .into_result();
        assert_eq!(result, Ok(Interval::Weekday(chrono::Weekday::Tue)));
    }
}