harper_core/expr/
time_unit_expr.rs

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