From 4de014c41ac84500f1360cec4a300fb5e15fe4c1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 10 Mar 2016 20:34:40 +0100 Subject: [PATCH] Add field_predicate filter --- .../src/builtin/header/field_predicate.rs | 45 +++++++++++++++++++ libimagentryfilter/src/builtin/header/mod.rs | 1 + 2 files changed, 46 insertions(+) create mode 100644 libimagentryfilter/src/builtin/header/field_predicate.rs 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 { + header_field_path: FieldPath, + predicate: Box

, +} + +impl FieldPredicate

{ + + pub fn new(path: FieldPath, predicate: Box

) -> FieldPredicate

{ + FieldPredicate { + header_field_path: path, + predicate: predicate, + } + } + +} + +impl Filter for FieldPredicate

{ + + 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;