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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use std::{
    fmt::Display,
    ops::{Add, Sub},
    str::FromStr,
};

use chrono::{
    DateTime, Datelike, Duration, Local, Month, NaiveDate, NaiveDateTime, NaiveTime, Weekday,
};
use pest::{iterators::{Pair, Pairs}, Parser};
use pest_derive::Parser;
use thiserror::Error;

#[derive(Parser)]
#[grammar = "date_time.pest"]
struct DateTimeParser;

#[derive(Debug, Error)]
pub enum ParseError {
    #[error("The data has a invalid format")]
    InvalidFormat,
    #[error("The value {amount} is invalid.")]
    ValueInvalid { amount: String },
    #[error("You gave a value of {value}. It was only allowed to be between {lower} and {upper}.")]
    ValueOutOfRange {
        lower: String,
        upper: String,
        value: String,
    },
}

#[cfg(not(test))]
/// Returns the current time in the local timezone.
macro_rules! now {
    () => {
        Local::now()
    };
}

#[cfg(test)]
/// If we are in a test enviroment we will just pretend it is 2010-01-01 00:00:00 right now.
macro_rules! now {
    () => {
        NaiveDateTime::parse_from_str("2010-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    };
}

pub enum ParseResult {
    DateTime(DateTime<Local>),
    Date(NaiveDate),
    Time(NaiveTime),
}

trait FindRule<'i> {
    fn find_one(&self, rule: Rule) -> Option<Pair<'i, Rule>>;
    fn find_all(&self, rule: Rule) -> Vec<Pair<'i, Rule>>;
}

impl<'i> FindRule<'i> for Pairs<'i, Rule> {
    fn find_one(&self, rule: Rule) -> Option<Pair<'i, Rule>> {
        self.to_owned().find(|x| x.as_rule() == rule)
    }

    fn find_all(&self, rule: Rule) -> Vec<Pair<'i, Rule>> {
        let s = self.to_owned();
        let mut vec: Vec<Pair<Rule>> = Vec::new();
        for pair in s {
            if pair.as_rule() == rule {
                vec.push(pair);
            }
        }

        vec
    }
}

trait ToVec<'a> {
    fn to_vec(self) -> Vec<Pair<'a, Rule>>;
}

impl<'a> ToVec<'a> for Pair<'a, Rule> {
    fn to_vec(self) -> Vec<Pair<'a, Rule>> {
        self.into_inner().collect()
    }
}

/// Converts a human expression of a date into a more usable one.
///
/// # Errors
///
/// This function will return an error if the string contains values than can not be parsed into a date.
///
/// # Examples
/// ```
/// use human_date_parser::{from_human_time, ParseResult};
///
/// fn main() {
///     let date = from_human_time("Last Friday at 19:45").unwrap();
///     match date {
///         ParseResult::DateTime(date) => println!("{date}"),
///         _ => unreachable!()
///     }
/// }
/// ```
pub fn from_human_time(str: &str) -> Result<ParseResult, ParseError> {
    let lowercase = str.to_lowercase();
    let mut parsed = match DateTimeParser::parse(Rule::HumanTime, &lowercase) {
        Ok(parsed) => parsed,
        Err(_) => return Err(ParseError::InvalidFormat),
    };

    let head = parsed.next().unwrap();
    let rule = head.as_rule();
    let result: ParseResult = match rule {
        Rule::DateTime => ParseResult::DateTime(parse_datetime(head)?),
        Rule::Date => ParseResult::Date(parse_date(head)?),
        Rule::Time => ParseResult::Time(parse_time(head)?),
        Rule::In | Rule::Ago => ParseResult::DateTime(parse_in_or_ago(head, rule)?),
        Rule::Now => ParseResult::DateTime(now!()),
        _ => unreachable!(),
    };

    Ok(result)
}

/// Parse a string DateTime element into it's chrono equivalent.
///
/// # Errors
///
/// This function will return an error if the pair contains values than can not be parsed into a date.
fn parse_datetime(head: Pair<Rule>) -> Result<DateTime<Local>, ParseError> {
    let date;
    let time;
    let mut iter = head.into_inner().into_iter();
    let first = iter.next().unwrap();
    let second = iter.next().unwrap();

    if first.as_rule() == Rule::Date {
        date = parse_date(first)?;
        time = parse_time(second)?;
    } else {
        date = parse_date(second)?;
        time = parse_time(first)?;
    }

    let date_time = NaiveDateTime::new(date, time);
    let date_time = date_time.and_local_timezone(Local).unwrap();
    Ok(date_time)
}

/// Parses a string in the 'In...' or '...ago' format into a valid DateTime.
///
/// # Errors
///
/// This function will return an error if the pair contains values than can not be parsed into a date.
fn parse_in_or_ago(head: Pair<Rule>, rule: Rule) -> Result<DateTime<Local>, ParseError> {
    let durations = collect_durations(head.into_inner().next().unwrap())?;
    let mut full_duration = Duration::zero();
    for duration in durations {
        full_duration = full_duration.add(duration);
    }
    Ok(match rule {
        Rule::In => now!() + full_duration,
        Rule::Ago => now!() - full_duration,
        _ => unreachable!(),
    })
}

/// Parses the date component of the string into a `NaiveDate`.
///
/// # Errors
///
/// This function will return an error if the pair contains values than can not be parsed into a `NaiveDate`.
fn parse_date(pair: Pair<Rule>) -> Result<NaiveDate, ParseError> {
    let inner_pairs: Vec<Pair<Rule>> = pair.to_vec();
    let first = inner_pairs.first().unwrap();

    match first.as_rule() {
        Rule::Today => Ok(now!().date_naive()),
        Rule::Tomorrow => Ok(now!().add(Duration::days(1)).date_naive()),
        Rule::Yesterday => Ok(now!().sub(Duration::days(1)).date_naive()),
        Rule::IsoDate => {
            let from_str = NaiveDate::from_str(first.as_str()).unwrap();
            Ok(from_str)
        }
        Rule::Num => {
            let day = parse_in_range(first.as_str(), 1, 31)?;
            let mut month_name = inner_pairs.get(1).unwrap().clone().into_inner();
            let month = month_from_rule(month_name.next().unwrap().as_rule()).number_from_month();
            let year = match inner_pairs.get(2) {
                Some(rule) => parse_in_range(rule.as_str(), 0, 10000)?,
                None => now!().year(),
            };

            let date = match NaiveDate::from_ymd_opt(year, month, day) {
                Some(date) => date,
                None => return Err(ParseError::InvalidFormat),
            };
            Ok(date)
        }
        Rule::RelativeSpecifier => {
            let specifier = first.clone().into_inner().next().unwrap().as_rule();
            let weekday_rule = inner_pairs.get(1).unwrap().clone();
            let specific_weekday = weekday_rule.into_inner().next().unwrap().as_rule();
            let weekday = weekday_from_rule(specific_weekday);
            Ok(find_weekday(weekday, specifier))
        }
        Rule::Weekday => {
            let specific_weekday = first.clone().into_inner().next().unwrap().as_rule();
            let weekday = weekday_from_rule(specific_weekday);
            Ok(find_weekday(weekday, Rule::This))
        }
        _ => unreachable!(),
    }
}

/// Finds the date for a given Weekday, either as this, next or last occurence of it.
///
/// # Panics
///
/// Panics if the rule given is not This, Next or Last.
fn find_weekday(weekday: Weekday, rule: Rule) -> NaiveDate {
    let current = now!().weekday().num_days_from_monday();
    let next = weekday.num_days_from_monday();
    let mut days_to_add: i64;

    if current <= next {
        days_to_add = (next - current).into();
    } else {
        days_to_add = (7 - (current - next)).into();
    }

    match rule {
        Rule::This => {}
        Rule::Next => days_to_add += 7,
        Rule::Last => days_to_add -= 7,
        _ => {
            panic!("Finding a weekday should only be done with This, Next or Last. This is a bug.")
        }
    }

    now!().date_naive() + Duration::days(days_to_add)
}

/// Parsed the time component into a `NaiveTime`.
///
/// # Errors
///
/// This function will return an error if the pair contains values than can not be parsed into `NaiveTime`.
fn parse_time(pair: Pair<Rule>) -> Result<NaiveTime, ParseError> {
    let time = match NaiveTime::parse_from_str(pair.as_str(), "%H:%M:%S") {
        Ok(time) => time,
        Err(_) => match NaiveTime::parse_from_str(pair.as_str(), "%H:%M") {
            Ok(time) => time,
            Err(_) => return Err(ParseError::InvalidFormat),
        },
    };
    Ok(time)
}

/// Parses a `str` into a number that is clamped withing the given lower and upper bound.
///
/// # Errors
///
/// This function will return an error if `str` could not be parsed or it is outside the given bounds.
fn parse_in_range<T>(str: &str, lower: T, upper: T) -> Result<T, ParseError>
where
    T: FromStr + PartialOrd<T> + Display,
{
    let value: T = match str.parse() {
        Ok(val) => val,
        Err(_) => return Err(ParseError::ValueInvalid { amount: str.into() }),
    };

    if value < lower || value > upper {
        return Err(ParseError::ValueOutOfRange {
            lower: lower.to_string(),
            upper: upper.to_string(),
            value: str.to_string(),
        });
    }

    Ok(value)
}

/// Parses all the durations in the Duration component and returns them as `Vec<Duration>`.
///
/// # Errors
///
/// This function will return an error if the pair contains invalid durations.
fn collect_durations(duration_rule: Pair<Rule>) -> Result<Vec<Duration>, ParseError> {
    let mut durations = Vec::new();

    for rule in duration_rule.into_inner() {
        match rule.as_rule() {
            Rule::Quantifier => {
                let mut amount: i64 = 0;
                let mut time_type = Rule::Minute;

                for inner in rule.into_inner() {
                    match inner.as_rule() {
                        Rule::Num => {
                            amount = match inner.as_str().parse() {
                                Ok(num) => num,
                                Err(_) => {
                                    return Err(ParseError::ValueInvalid {
                                        amount: inner.as_str().into(),
                                    })
                                }
                            }
                        }
                        Rule::TimeUnit => time_type = inner.into_inner().next().unwrap().as_rule(),
                        _ => unreachable!(),
                    }
                }

                durations.push(create_duration(time_type, amount)?);
            }
            Rule::SingleUnit => {
                for inner in rule.into_inner() {
                    if inner.as_rule() == Rule::TimeUnit {
                        durations.push(create_duration(
                            inner.into_inner().next().unwrap().as_rule(),
                            1,
                        )?);
                    }
                }
            }
            _ => unreachable!(),
        }
    }

    Ok(durations)
}

/// Combines the rule and amount into a `Duration`.
///
/// # Errors
///
/// This function will return an error if the pair contains values than can not be parsed into a `Duration`.
fn create_duration(rule: Rule, amount: i64) -> Result<Duration, ParseError> {
    let dur = match rule {
        Rule::Year => {
            let now = now!();
            let years: i32 = match amount.try_into() {
                Ok(years) => years,
                Err(_) => {
                    return Err(ParseError::ValueInvalid {
                        amount: amount.to_string(),
                    })
                }
            };
            let next_year = match now.with_year(now.year() + years) {
                Some(year) => year,
                None => {
                    return Err(ParseError::ValueInvalid {
                        amount: amount.to_string(),
                    })
                }
            };
            next_year - now
        }
        Rule::Month => {
            let now = now!();
            let months: u32 = match amount.try_into() {
                Ok(months) => months,
                Err(_) => {
                    return Err(ParseError::ValueInvalid {
                        amount: amount.to_string(),
                    })
                }
            };
            let next_month = match now.with_month0((now.month0() + months) % 12) {
                Some(month) => month,
                None => {
                    return Err(ParseError::ValueInvalid {
                        amount: amount.to_string(),
                    })
                }
            };

            next_month - now
        }
        Rule::Week => Duration::days(amount * 7),
        Rule::Day => Duration::days(amount),
        Rule::Hour => Duration::hours(amount),
        Rule::Minute => Duration::minutes(amount),
        Rule::Second => Duration::seconds(amount),
        _ => unreachable!(),
    };

    Ok(dur)
}

/// Returns the `chrono::month` equivalent of a parser rule.
///
/// # Panics
///
/// Panics if the given rule does not correspond to a month.
fn month_from_rule(rule: Rule) -> Month {
    match rule {
        Rule::January => Month::January,
        Rule::February => Month::February,
        Rule::March => Month::March,
        Rule::April => Month::April,
        Rule::May => Month::May,
        Rule::June => Month::June,
        Rule::July => Month::July,
        Rule::August => Month::August,
        Rule::September => Month::September,
        Rule::October => Month::October,
        Rule::November => Month::November,
        Rule::December => Month::December,
        _ => panic!("Tried to convert something that isn't a month to a month. This is a bug. Tried to convert: {:?}", rule),
    }
}

/// Returns the `chrono::weekday` equivalent of a parser rule.
///
/// # Panics
///
/// Panics if the given rule does not correspond to a weekday.
fn weekday_from_rule(rule: Rule) -> Weekday {
    match rule {
        Rule::Monday => Weekday::Mon,
        Rule::Tuesday => Weekday::Tue,
        Rule::Wednesday => Weekday::Wed,
        Rule::Thursday => Weekday::Thu,
        Rule::Friday => Weekday::Fri,
        Rule::Saturday => Weekday::Sat,
        Rule::Sunday => Weekday::Sun,
        _ => {
            panic!("Tried to convert {rule:?} to a weekday, which is not possible. This is a bug.")
        }
    }
}

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

    /// Generates the test cases to remove a bunch of boilerplate code for the test setup.
    macro_rules! generate_test_cases {
        ( $( $case:literal = $expected:literal ),* ) => {
            $(
                concat_idents!(fn_name = parse_, $case {
                    #[test]
                    fn fn_name () {
                        let input = $case.to_lowercase();
                        let result = from_human_time(&input).unwrap();
                        let expected = NaiveDateTime::parse_from_str( $expected , "%Y-%m-%d %H:%M:%S").unwrap().and_local_timezone(Local).unwrap();

                        let result = match result {
                            ParseResult::DateTime(datetime) => datetime,
                            ParseResult::Date(date) => NaiveDateTime::new(date, now!().time()).and_local_timezone(Local).unwrap(),
                            ParseResult::Time(time) => NaiveDateTime::new(now!().date_naive(), time).and_local_timezone(Local).unwrap(),
                        };

                        println!("Result: {result}\nExpected: {expected}\nNote: Maximum difference between these values allowed is 10ms.");
                        assert!(result - expected < Duration::milliseconds(10));
                    }
                });
            )*
        };
    }

    generate_test_cases!(
        "Today 18:30" = "2010-01-01 18:30:00",
        "2022-11-07 13:25:30" = "2022-11-07 13:25:30",
        "15:20 Friday" = "2010-01-08 15:20:00",
        "This Friday 17:00" = "2010-01-08 17:00:00",
        "13:25, Next Tuesday" = "2010-01-12 13:25:00",
        "Last Friday at 19:45" = "2009-12-25 19:45:00",
        "In 3 days" = "2010-01-04 00:00:00",
        "In 2 hours" = "2010-01-01 02:00:00",
        "In 5 minutes and 30 seconds" = "2010-01-01 00:05:30",
        "10 seconds ago" = "2009-12-31 23:59:50",
        "10 hours and 5 minutes ago" = "2009-12-31 13:55:00",
        "2 hours, 32 minutes and 7 seconds ago" = "2009-12-31 21:27:53",
        "1 years, 2 months, 3 weeks, 5 days, 8 hours, 17 minutes and 45 seconds ago" =
            "2008-10-07 16:42:15",
        "1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute and 1 second ago" = "2008-11-23 22:58:59",
        "A year ago" = "2009-01-01 00:00:00",
        "A month ago" = "2009-12-01 00:00:00",
        "A week ago" = "2009-12-25 00:00:00",
        "A day ago" = "2009-12-31 00:00:00",
        "An hour ago" = "2009-12-31 23:00:00",
        "A minute ago" = "2009-12-31 23:59:00",
        "A second ago" = "2009-12-31 23:59:59",
        "now" = "2010-01-01 00:00:00"
    );
}