Add field_predicate filter

This commit is contained in:
Matthias Beyer 2016-03-10 20:34:40 +01:00
parent 9a918c9252
commit 4de014c41a
2 changed files with 46 additions and 0 deletions

View file

@ -0,0 +1,45 @@
use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use filter::Filter;
use toml::Value;
pub trait Predicate {
fn evaluate(&self, Value) -> bool;
}
/// Check whether certain header field in a entry is equal to a value
pub struct FieldPredicate<P: Predicate> {
header_field_path: FieldPath,
predicate: Box<P>,
}
impl<P: Predicate> FieldPredicate<P> {
pub fn new(path: FieldPath, predicate: Box<P>) -> FieldPredicate<P> {
FieldPredicate {
header_field_path: path,
predicate: predicate,
}
}
}
impl<P: Predicate> Filter for FieldPredicate<P> {
fn filter(&self, e: &Entry) -> bool {
e.get_header()
.read(&self.header_field_path[..])
.map(|val| {
match val {
None => false,
Some(v) => (*self.predicate).evaluate(v),
}
})
.unwrap_or(false)
}
}

View file

@ -4,3 +4,4 @@ pub mod field_grep;
pub mod field_isempty; pub mod field_isempty;
pub mod field_istype; pub mod field_istype;
pub mod field_path; pub mod field_path;
pub mod field_predicate;