diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-26 17:41:20 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-26 17:57:56 +0200 |
commit | 4be0e3b361615a84ecf1402b827a81ca62e95bc5 (patch) | |
tree | 900ca070850269155815499b88307566cecd0a50 | |
parent | 77c3f75343dc84b87e99e333bc15ea5c308fc017 (diff) |
Implement filter interface for matchers
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | src/indicator.rs | 42 | ||||
-rw-r--r-- | src/iter.rs | 17 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/matcher.rs | 11 |
5 files changed, 80 insertions, 0 deletions
@@ -15,3 +15,10 @@ repository = "https://github.com/matthiasbeyer/kairos" [dependencies] chrono = "0.4" error-chain = "0.10" + +filters = { version = "0.1.1", optional = true } + +[features] +default = [] +with-filters = [ "filters" ] + diff --git a/src/indicator.rs b/src/indicator.rs index 7a57b7e..72e1136 100644 --- a/src/indicator.rs +++ b/src/indicator.rs @@ -57,3 +57,45 @@ impl Into<u32> for Month { } } } + +#[cfg(feature = "with-filters")] +pub struct DayFilter(Day); + +#[cfg(feature = "with-filters")] +impl Filter<TimeType> for DayFilter { + fn filter(&self, tt: &TimeType) -> bool { + tt.get_moment(|mom| mom.weekday() == self.0.into()).unwrap_or(false) + } +} + +#[cfg(feature = "with-filters")] +impl IntoFilter<TimeType> for Day { + type IntoFilt = DayFilter; + + fn into_filter(self) -> Self::IntoFilt { + DayFilter(self) + } + +} + + +#[cfg(feature = "with-filters")] +pub struct MonthFilter(Month); + +#[cfg(feature = "with-filters")] +impl Filter<TimeType> for MonthFilter { + fn filter(&self, tt: &TimeType) -> bool { + tt.get_moment(|mom| mom.month() == self.0.into()).unwrap_or(false) + } +} + +#[cfg(feature = "with-filters")] +impl IntoFilter<TimeType> for Month { + type IntoFilt = MonthFilter; + + fn into_filter(self) -> Self::IntoFilt { + MonthFilter(self) + } + +} + diff --git a/src/iter.rs b/src/iter.rs index 7e53da4..0b8c6a0 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -412,3 +412,20 @@ mod type_tests { } } +#[cfg(all(feature = "with-filters", test))] +mod type_tests_filter_interface { + use super::*; + use super::IntoCalculatingIter; + use super::extensions::*; + + #[test] + fn test_compile() { + // This test is solely to check whether this compiles and the API is nice + let _ = TimeType::today() + .yearly(1) + .unwrap() + .calculate() + .every(::indicator::Day::Monday.or(::indicator::Month::January)); + } +} + @@ -2,6 +2,9 @@ extern crate error_chain; extern crate chrono; +#[cfg(feature = "with-filters")] +extern crate filters; + pub mod error; pub mod iter; pub mod result; diff --git a/src/matcher.rs b/src/matcher.rs index 1482b68..d574090 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -34,4 +34,15 @@ impl Matcher for Month { } +#[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)) + } +} |