diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-03-10 20:34:40 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-03-12 15:06:33 +0100 |
commit | 4de014c41ac84500f1360cec4a300fb5e15fe4c1 (patch) | |
tree | c83cd04959ee7d948b6a2b866cb9a513f850ecb7 /libimagentryfilter | |
parent | 9a918c925225d6cf241aca43e4a90167efbf6b60 (diff) |
Add field_predicate filter
Diffstat (limited to 'libimagentryfilter')
-rw-r--r-- | libimagentryfilter/src/builtin/header/field_predicate.rs | 45 | ||||
-rw-r--r-- | libimagentryfilter/src/builtin/header/mod.rs | 1 |
2 files changed, 46 insertions, 0 deletions
diff --git a/libimagentryfilter/src/builtin/header/field_predicate.rs b/libimagentryfilter/src/builtin/header/field_predicate.rs new file mode 100644 index 00000000..5dddc46e --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_predicate.rs @@ -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) + } + +} + + diff --git a/libimagentryfilter/src/builtin/header/mod.rs b/libimagentryfilter/src/builtin/header/mod.rs index 886e5ce6..c3725738 100644 --- a/libimagentryfilter/src/builtin/header/mod.rs +++ b/libimagentryfilter/src/builtin/header/mod.rs @@ -4,3 +4,4 @@ pub mod field_grep; pub mod field_isempty; pub mod field_istype; pub mod field_path; +pub mod field_predicate; |