summaryrefslogtreecommitdiffstats
path: root/src/matcher.rs
blob: ca4fb8dd5138c3f2c8ab831f16e5705fa97dac43 (plain)
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
use chrono::Datelike;

use error::ErrorKind as KEK;
use failure::Fallible as Result;
use failure::Error;
use indicator::Day;
use indicator::Month;
use timetype::TimeType;

/// A trait to extend indicator::* to be able to match them with a TimeType object
pub trait Matcher {
    fn matches(&self, tt: &TimeType) -> Result<bool>;
}

impl Matcher for Day {

    fn matches(&self, tt: &TimeType) -> Result<bool> {
        let this : ::chrono::Weekday = self.clone().into();
        tt.get_moment()
            .map(|mom| this == mom.weekday())
            .ok_or(Error::from(KEK::ArgumentErrorNotAMoment(tt.name())))
    }
}

impl Matcher for Month {

    fn matches(&self, tt: &TimeType) -> Result<bool> {
        let this : u32 = self.clone().into();
        tt.get_moment()
            .map(|mom| this == mom.month())
            .ok_or(Error::from(KEK::ArgumentErrorNotAMoment(tt.name())))
    }

}

#[cfg(feature = "with-filters")]
use filters::filter::*;

#[cfg(feature = "with-filters")]
impl<F> Matcher for F
    where F: Filter<TimeType>
{
    fn matches(&self, tt: &TimeType) -> Result<bool> {
        Ok(self.filter(tt))
    }
}