diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-26 17:58:38 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-26 17:58:38 +0200 |
commit | 874fcc34d50d6d69e30cc3ce6ef14e617354fe4c (patch) | |
tree | 900ca070850269155815499b88307566cecd0a50 /src/matcher.rs | |
parent | 79923c0cd6ed917b3b0463a66d0a44df85165e23 (diff) | |
parent | 4be0e3b361615a84ecf1402b827a81ca62e95bc5 (diff) |
Merge branch 'more-iter'
Diffstat (limited to 'src/matcher.rs')
-rw-r--r-- | src/matcher.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/matcher.rs b/src/matcher.rs new file mode 100644 index 0000000..d574090 --- /dev/null +++ b/src/matcher.rs @@ -0,0 +1,48 @@ + +use chrono::Datelike; + +use error::KairosError as KE; +use error::KairosErrorKind as KEK; +use error::Result; +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(KE::from_kind(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(KE::from_kind(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)) + } +} + |