From 0a9416089530d613e14441ea9dbbd221311ba7ec Mon Sep 17 00:00:00 2001 From: Kai Sickeler Date: Sat, 12 Mar 2016 17:39:57 +0100 Subject: [PATCH] Add field_grep filter Signed-off-by: Matthias Beyer --- .../src/builtin/header/field_grep.rs | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/libimagentryfilter/src/builtin/header/field_grep.rs b/libimagentryfilter/src/builtin/header/field_grep.rs index 2c28c9cf..aac42207 100644 --- a/libimagentryfilter/src/builtin/header/field_grep.rs +++ b/libimagentryfilter/src/builtin/header/field_grep.rs @@ -4,20 +4,35 @@ use toml::Value; use libimagstore::store::Entry; use builtin::header::field_path::FieldPath; +use builtin::header::field_predicate::FieldPredicate; +use builtin::header::field_predicate::Predicate; use filter::Filter; +struct EqGrep{ + regex: Regex +} + +impl Predicate for EqGrep { + + fn evaluate(&self, v: Value) -> bool { + match v { + Value::String(s) => self.regex.captures(&s[..]).is_some(), + _ => false, + } + } + +} + /// Check whether certain header field in a entry is equal to a value pub struct FieldGrep { - header_field_path: FieldPath, - grep: Regex, + filter: FieldPredicate, } impl FieldGrep { pub fn new(path: FieldPath, grep: Regex) -> FieldGrep { FieldGrep { - header_field_path: path, - grep: grep, + filter: FieldPredicate::new(path, Box::new(EqGrep { regex: grep})), } } @@ -26,15 +41,7 @@ impl FieldGrep { impl Filter for FieldGrep { fn filter(&self, e: &Entry) -> bool { - e.get_header() - .read(&self.header_field_path[..]) - .map(|v| { - match v { - Some(Value::String(s)) => self.grep.captures(&s[..]).is_some(), - _ => false, - } - }) - .unwrap_or(false) + self.filter.filter(e) } }