diff --git a/libimagentrytimetrack/src/iter/filter.rs b/libimagentrytimetrack/src/iter/filter.rs index b8299b46..0a3ae538 100644 --- a/libimagentrytimetrack/src/iter/filter.rs +++ b/libimagentrytimetrack/src/iter/filter.rs @@ -19,123 +19,106 @@ use result::Result; +use chrono::NaiveDateTime; +use filters::filter::Filter; + use libimagstore::store::FileLockEntry; use tag::TimeTrackingTag as TTT; use timetracking::TimeTracking; -pub struct WithOneOf<'a, I> - where I: Iterator>> -{ - iter: I, - allowed_tags: &'a Vec, + +pub fn has_start_time(entry: &FileLockEntry) -> bool { + is_match!(entry.get_start_datetime(), Ok(Some(_))) } -impl<'a, I> WithOneOf<'a, I> - where I: Iterator>> -{ +pub fn has_end_time(entry: &FileLockEntry) -> bool { + is_match!(entry.get_end_datetime(), Ok(Some(_))) +} - pub fn new(iter: I, allowed_tags: &'a Vec) -> WithOneOf<'a, I> { - WithOneOf { - iter: iter, - allowed_tags: allowed_tags +pub fn has_tag(entry: &FileLockEntry) -> bool { + is_match!(entry.get_timetrack_tag(), Ok(_)) +} + +pub fn has_start_time_where(f: F) -> HasStartTimeWhere + where F: Fn(&NaiveDateTime) -> bool +{ + HasStartTimeWhere::new(f) +} + +pub fn has_end_time_where(f: F) -> HasEndTimeWhere + where F: Fn(&NaiveDateTime) -> bool +{ + HasEndTimeWhere::new(f) +} + +pub fn has_one_of_tags<'a>(tags: &'a Vec) -> HasOneOfTags<'a> { + HasOneOfTags::new(tags) +} + +mod types { + use chrono::NaiveDateTime; + use filters::filter::Filter; + + use tag::TimeTrackingTag as TTT; + use timetracking::TimeTracking; + + use libimagstore::store::FileLockEntry; + + pub struct HasStartTimeWhere(F) + where F: Fn(&NaiveDateTime) -> bool; + + impl bool> HasStartTimeWhere { + pub fn new(f: F) -> HasStartTimeWhere { + HasStartTimeWhere(f) } } -} -impl<'a, I> Iterator for WithOneOf<'a, I> - where I: Iterator>> -{ - type Item = Result>; - - fn next(&mut self) -> Option { - loop { - match self.iter.next() { - Some(Ok(fle)) => { - match fle.get_timetrack_tag() { - Err(e) => return Some(Err(e)), - Ok(t) => if self.allowed_tags.contains(&t) { - return Some(Ok(fle)) - } else { - // loop - }, - } - }, - Some(Err(e)) => return Some(Err(e)), - None => return None, - } + impl<'a, F> Filter> for HasStartTimeWhere + where F: Fn(&NaiveDateTime) -> bool + { + fn filter(&self, entry: &FileLockEntry) -> bool { + entry.get_start_datetime() + .map(|o| o.map(|dt| (self.0)(&dt)).unwrap_or(false)) + .unwrap_or(false) } } -} -pub trait WithOneOfTags<'a> : Sized + Iterator>> { - fn with_timetracking_tags(self, tags: &'a Vec) -> WithOneOf<'a, Self>; -} + pub struct HasEndTimeWhere(F) + where F: Fn(&NaiveDateTime) -> bool; -impl<'a, I> WithOneOfTags<'a> for I - where I: Iterator>>, - Self: Sized -{ - fn with_timetracking_tags(self, tags: &'a Vec) -> WithOneOf<'a, Self> { - WithOneOf::new(self, tags) - } -} - - -pub struct WithNoneOf<'a, I> - where I: Iterator>> -{ - iter: I, - disallowed_tags: &'a Vec, -} - -impl<'a, I> WithNoneOf<'a, I> - where I: Iterator>> -{ - - pub fn new(iter: I, disallowed_tags: &'a Vec) -> WithNoneOf<'a, I> { - WithNoneOf { - iter: iter, - disallowed_tags: disallowed_tags + impl bool> HasEndTimeWhere { + pub fn new(f: F) -> HasEndTimeWhere { + HasEndTimeWhere(f) } } -} -impl<'a, I> Iterator for WithNoneOf<'a, I> - where I: Iterator>> -{ - type Item = Result>; - - fn next(&mut self) -> Option { - loop { - match self.iter.next() { - Some(Ok(fle)) => { - match fle.get_timetrack_tag() { - Err(e) => return Some(Err(e)), - Ok(t) => if !self.disallowed_tags.contains(&t) { - return Some(Ok(fle)) - } else { - // loop - }, - } - }, - Some(Err(e)) => return Some(Err(e)), - None => return None, - } + impl<'a, F> Filter> for HasEndTimeWhere + where F: Fn(&NaiveDateTime) -> bool + { + fn filter(&self, entry: &FileLockEntry) -> bool { + entry.get_end_datetime() + .map(|o| o.map(|dt| (self.0)(&dt)).unwrap_or(false)) + .unwrap_or(false) } } -} -pub trait WithNoneOfTags<'a> : Sized + Iterator>> { - fn without_timetracking_tags(self, tags: &'a Vec) -> WithNoneOf<'a, Self>; -} + pub struct HasOneOfTags<'a>(&'a Vec); -impl<'a, I> WithNoneOfTags<'a> for I - where I: Iterator>>, - Self: Sized -{ - fn without_timetracking_tags(self, tags: &'a Vec) -> WithNoneOf<'a, Self> { - WithNoneOf::new(self, tags) + impl<'a> HasOneOfTags<'a> { + pub fn new(tags: &'a Vec) -> HasOneOfTags<'a> { + HasOneOfTags(tags) + } } -} + + impl<'a, 'b> Filter> for HasOneOfTags<'a> { + fn filter(&self, entry: &FileLockEntry) -> bool { + entry.get_timetrack_tag().map(|t| self.0.contains(&t)).unwrap_or(false) + } + } + +} +pub use self::types::HasStartTimeWhere; +pub use self::types::HasEndTimeWhere; +pub use self::types::HasOneOfTags;