Start reimplementing filter module with filters crate

This commit is contained in:
Matthias Beyer 2017-07-20 12:12:11 +02:00
parent a98da2c01c
commit e2e42232e6

View file

@ -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<Item = Result<FileLockEntry<'a>>>
pub fn has_start_time(entry: &FileLockEntry) -> bool {
is_match!(entry.get_start_datetime(), Ok(Some(_)))
}
pub fn has_end_time(entry: &FileLockEntry) -> bool {
is_match!(entry.get_end_datetime(), Ok(Some(_)))
}
pub fn has_tag(entry: &FileLockEntry) -> bool {
is_match!(entry.get_timetrack_tag(), Ok(_))
}
pub fn has_start_time_where<F>(f: F) -> HasStartTimeWhere<F>
where F: Fn(&NaiveDateTime) -> bool
{
iter: I,
allowed_tags: &'a Vec<TTT>,
HasStartTimeWhere::new(f)
}
impl<'a, I> WithOneOf<'a, I>
where I: Iterator<Item = Result<FileLockEntry<'a>>>
pub fn has_end_time_where<F>(f: F) -> HasEndTimeWhere<F>
where F: Fn(&NaiveDateTime) -> bool
{
pub fn new(iter: I, allowed_tags: &'a Vec<TTT>) -> WithOneOf<'a, I> {
WithOneOf {
iter: iter,
allowed_tags: allowed_tags
}
}
HasEndTimeWhere::new(f)
}
impl<'a, I> Iterator for WithOneOf<'a, I>
where I: Iterator<Item = Result<FileLockEntry<'a>>>
{
type Item = Result<FileLockEntry<'a>>;
fn next(&mut self) -> Option<Self::Item> {
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,
}
}
}
pub fn has_one_of_tags<'a>(tags: &'a Vec<TTT>) -> HasOneOfTags<'a> {
HasOneOfTags::new(tags)
}
pub trait WithOneOfTags<'a> : Sized + Iterator<Item = Result<FileLockEntry<'a>>> {
fn with_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithOneOf<'a, Self>;
}
impl<'a, I> WithOneOfTags<'a> for I
where I: Iterator<Item = Result<FileLockEntry<'a>>>,
Self: Sized
{
fn with_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithOneOf<'a, Self> {
WithOneOf::new(self, tags)
}
}
pub struct WithNoneOf<'a, I>
where I: Iterator<Item = Result<FileLockEntry<'a>>>
{
iter: I,
disallowed_tags: &'a Vec<TTT>,
}
impl<'a, I> WithNoneOf<'a, I>
where I: Iterator<Item = Result<FileLockEntry<'a>>>
{
pub fn new(iter: I, disallowed_tags: &'a Vec<TTT>) -> WithNoneOf<'a, I> {
WithNoneOf {
iter: iter,
disallowed_tags: disallowed_tags
}
}
}
impl<'a, I> Iterator for WithNoneOf<'a, I>
where I: Iterator<Item = Result<FileLockEntry<'a>>>
{
type Item = Result<FileLockEntry<'a>>;
fn next(&mut self) -> Option<Self::Item> {
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,
}
}
}
}
pub trait WithNoneOfTags<'a> : Sized + Iterator<Item = Result<FileLockEntry<'a>>> {
fn without_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithNoneOf<'a, Self>;
}
impl<'a, I> WithNoneOfTags<'a> for I
where I: Iterator<Item = Result<FileLockEntry<'a>>>,
Self: Sized
{
fn without_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithNoneOf<'a, Self> {
WithNoneOf::new(self, 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>(F)
where F: Fn(&NaiveDateTime) -> bool;
impl<F: Fn(&NaiveDateTime) -> bool> HasStartTimeWhere<F> {
pub fn new(f: F) -> HasStartTimeWhere<F> {
HasStartTimeWhere(f)
}
}
impl<'a, F> Filter<FileLockEntry<'a>> for HasStartTimeWhere<F>
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 struct HasEndTimeWhere<F>(F)
where F: Fn(&NaiveDateTime) -> bool;
impl<F: Fn(&NaiveDateTime) -> bool> HasEndTimeWhere<F> {
pub fn new(f: F) -> HasEndTimeWhere<F> {
HasEndTimeWhere(f)
}
}
impl<'a, F> Filter<FileLockEntry<'a>> for HasEndTimeWhere<F>
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 struct HasOneOfTags<'a>(&'a Vec<TTT>);
impl<'a> HasOneOfTags<'a> {
pub fn new(tags: &'a Vec<TTT>) -> HasOneOfTags<'a> {
HasOneOfTags(tags)
}
}
impl<'a, 'b> Filter<FileLockEntry<'b>> 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;