Add Filter trait and operators

This commit is contained in:
Matthias Beyer 2016-02-02 14:48:05 +01:00
parent 10050db42f
commit 0fb331f25a
4 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,30 @@
use libimagstore::store::Entry;
pub use ops::and::And;
pub use ops::not::Not;
pub use ops::or::Or;
pub trait Filter {
fn filter(&self, &Entry) -> bool;
fn not(self) -> Not
where Self: Sized + 'static
{
Not::new(Box::new(self))
}
fn or(self, other: Box<Filter>) -> Or
where Self: Sized + 'static
{
Or::new(Box::new(self), other)
}
fn and(self, other: Box<Filter>) -> And
where Self: Sized + 'static
{
And::new(Box::new(self), other)
}
}

View file

@ -0,0 +1,24 @@
use libimagstore::store::Entry;
use filter::Filter;
pub struct And {
a: Box<Filter>,
b: Box<Filter>
}
impl And {
pub fn new(a: Box<Filter>, b: Box<Filter>) -> And {
And { a: a, b: b }
}
}
impl Filter for And {
fn filter(&self, e: &Entry) -> bool {
self.a.filter(e) && self.b.filter(e)
}
}

View file

@ -0,0 +1,23 @@
use libimagstore::store::Entry;
use filter::Filter;
pub struct Not {
a: Box<Filter>
}
impl Not {
pub fn new(a: Box<Filter>) -> Not {
Not { a: a }
}
}
impl Filter for Not {
fn filter(&self, e: &Entry) -> bool {
!self.a.filter(e)
}
}

View file

@ -0,0 +1,24 @@
use libimagstore::store::Entry;
use filter::Filter;
pub struct Or {
a: Box<Filter>,
b: Box<Filter>
}
impl Or {
pub fn new(a: Box<Filter>, b: Box<Filter>) -> Or {
Or { a: a, b: b }
}
}
impl Filter for Or {
fn filter(&self, e: &Entry) -> bool {
self.a.filter(e) || self.b.filter(e)
}
}