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
use crate::rules::errors::DateTimeError;
use crate::rules::rules::{Context, MatchResult};
use chrono::offset::{TimeZone, Utc};
use chrono::{DateTime, Datelike, NaiveDateTime, Timelike};

type ParserType<'a, Tz> =
    Fn(DateTime<Tz>, &'a str, bool) -> Vec<Result<MatchResult, DateTimeError>>;

pub struct Parser<'a, Tz: TimeZone + 'a> {
    lang_parser:
        Box<Fn(DateTime<Tz>, &'a str, bool) -> Vec<Result<MatchResult, DateTimeError>> + 'a>,
    exact_match: bool,
    max_dist: usize,
    tz: Tz,
}

impl<'a, Tz: TimeZone + 'a> Parser<'a, Tz> {
    pub fn new(tz: Tz) -> Self {
        Parser {
            lang_parser: Box::new(super::en),
            exact_match: false,
            max_dist: 5,
            tz,
        }
    }

    pub fn parser(mut self, parser_func: Box<ParserType<'a, Tz>>) -> Self {
        self.lang_parser = parser_func;
        self
    }

    pub fn max_dist(mut self, max_dist: usize) -> Self {
        self.max_dist = max_dist;
        self
    }

    pub fn fuzzy_parse(mut self, fuzzy_parse: bool) -> Self {
        self.exact_match = !fuzzy_parse;
        self
    }

    pub fn get_tz(&self) -> &Tz {
        &self.tz
    }

    fn parser_helper(
        &self,
        now: NaiveDateTime,
        input: &'a str,
    ) -> (DateTime<Tz>, Vec<Result<Context, DateTimeError>>) {
        let tz_aware = self.tz.from_utc_datetime(&now);

        let res = (self.lang_parser)(tz_aware.clone(), input, self.exact_match);
        let merged = self.merge(res);

        (tz_aware, merged)
    }

    // convert date/time to chrono
    pub fn parse(&self, input: &'a str) -> Vec<Result<DateTime<Tz>, DateTimeError>> {
        let (tz_aware, merged) = self.parser_helper(Utc::now().naive_utc(), input);
        self.to_chrono(tz_aware, merged)
    }

    // convert date/time to chrono
    pub fn parse_fixed_time(
        &self,
        now: NaiveDateTime,
        input: &'a str,
    ) -> Vec<Result<DateTime<Tz>, DateTimeError>> {
        let (tz_aware, merged) = self.parser_helper(now, input);
        self.to_chrono(tz_aware, merged)
    }

    fn merge_group(&self, group: &[&MatchResult]) -> Context {
        let mut result = Context::default();
        for item in group.iter() {
            result.update(item.get_timeshift());
        }
        result
    }

    fn merge(
        &self,
        parsed: Vec<Result<MatchResult, DateTimeError>>,
    ) -> Vec<Result<Context, DateTimeError>> {
        let mut group: Vec<&MatchResult> = Vec::new();
        let mut merged = Vec::new();

        // parse results are ordered from leftmost match to the rightmost
        for item in parsed.iter() {
            match item {
                Ok(match_result) => {
                    let last = group.last();
                    if last.is_some()
                        && match_result.get_start_idx() - last.unwrap().get_end_idx()
                            > self.max_dist
                    {
                        // distance is bigger than allowed threshold, finish previous group
                        merged.push(Ok(self.merge_group(&group)));
                        group.clear();
                    }
                    // and start building a new one
                    group.push(match_result);
                }
                Err(e) => {
                    merged.push(Err(e.clone()));
                    group.clear();
                }
            }
        }

        // merge everything that left
        if !group.is_empty() {
            merged.push(Ok(self.merge_group(&group)));
        }

        merged
    }

    fn to_chrono(
        &self,
        date_time: DateTime<Tz>,
        merged: Vec<Result<Context, DateTimeError>>,
    ) -> Vec<Result<DateTime<Tz>, DateTimeError>> {
        let mut ready: Vec<Result<DateTime<Tz>, DateTimeError>> = Vec::new();

        for ctx in merged {
            if ctx.is_err() {
                ready.push(Err(ctx.unwrap_err()));
                continue;
            }

            let ctx = ctx.unwrap();

            let mut tz_aware = date_time.clone();

            tz_aware = tz_aware + ctx.duration;

            if ctx.duration.num_seconds() % 60 == 0 {
                tz_aware = tz_aware.with_second(0).unwrap();
            }

            if ctx.year.is_some() {
                tz_aware = tz_aware.with_year(ctx.year.unwrap()).unwrap();
            }
            if ctx.month.is_some() {
                tz_aware = tz_aware.with_month(ctx.month.unwrap() as u32).unwrap();
            }
            if ctx.day.is_some() {
                tz_aware = tz_aware.with_day(ctx.day.unwrap() as u32).unwrap();
            }
            if ctx.hour.is_some() {
                tz_aware = tz_aware.with_hour(ctx.hour.unwrap() as u32).unwrap();
            }
            if ctx.minute.is_some() {
                tz_aware = tz_aware.with_minute(ctx.minute.unwrap() as u32).unwrap();
            }

            tz_aware = tz_aware.with_nanosecond(0).unwrap();

            ready.push(Ok(tz_aware));
        }
        ready
    }
}