Add Filter trait and operators
This commit is contained in:
parent
10050db42f
commit
0fb331f25a
4 changed files with 101 additions and 0 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue