harper_core/patterns/
time_unit_pattern.rs

1use crate::Token;
2
3use super::{LongestMatchOf, Pattern, WordSet};
4
5/// Matches a time unit.
6///
7/// Matches standard units from microsecond to decade.
8/// Matches other 'units' such as moment, night, weekend.
9/// Matches singular and plural forms.
10/// Matches possessive forms (which are also common misspellings for the plurals).
11/// Matches abbreviations.
12#[derive(Default)]
13pub struct TimeUnitPattern;
14
15impl Pattern for TimeUnitPattern {
16    fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
17        if tokens.is_empty() {
18            return None;
19        }
20
21        let units_definite_singular = WordSet::new(&[
22            "microsecond",
23            "millisecond",
24            "second",
25            "minute",
26            "hour",
27            "day",
28            "week",
29            "month",
30            "year",
31            "decade",
32        ]);
33
34        let units_definite_plural = WordSet::new(&[
35            "microseconds",
36            "milliseconds",
37            "seconds",
38            "minutes",
39            "hours",
40            "days",
41            "weeks",
42            "months",
43            "years",
44            "decades",
45        ]);
46
47        let units_definite_apos = WordSet::new(&[
48            "microsecond's",
49            "millisecond's",
50            "second's",
51            "minute's",
52            "hour's",
53            "day's",
54            "week's",
55            "month's",
56            "year's",
57            "decade's",
58        ]);
59
60        // ms
61        let units_definite_abbrev = WordSet::new(&["ms"]);
62
63        let units_other_singular = WordSet::new(&["moment", "night", "weekend"]);
64        let units_other_plural = WordSet::new(&["moments", "nights", "weekends"]);
65        let units_other_apos = WordSet::new(&["moment's", "night's", "weekend's"]);
66
67        let units = LongestMatchOf::new(vec![
68            Box::new(units_definite_singular),
69            Box::new(units_definite_plural),
70            Box::new(units_other_singular),
71            Box::new(units_other_plural),
72            Box::new(units_definite_abbrev),
73            Box::new(units_definite_apos),
74            Box::new(units_other_apos),
75        ]);
76
77        units.matches(tokens, source)
78    }
79}