diff --git a/libimagentryfilter/src/builtin/header/field_eq.rs b/libimagentryfilter/src/builtin/header/field_eq.rs index 5617a42b..eeef5aa7 100644 --- a/libimagentryfilter/src/builtin/header/field_eq.rs +++ b/libimagentryfilter/src/builtin/header/field_eq.rs @@ -1,22 +1,34 @@ 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; use toml::Value; +struct EqPred { + expected: Value +} + +impl Predicate for EqPred { + + fn evaluate(&self, v: Value) -> bool { + self.expected == v + } + +} + /// Check whether certain header field in a entry is equal to a value pub struct FieldEq { - header_field_path: FieldPath, - expected_value: Value + filter: FieldPredicate, } impl FieldEq { pub fn new(path: FieldPath, expected_value: Value) -> FieldEq { FieldEq { - header_field_path: path, - expected_value: expected_value, + filter: FieldPredicate::new(path, Box::new(EqPred { expected: expected_value })), } } @@ -25,10 +37,7 @@ impl FieldEq { impl Filter for FieldEq { fn filter(&self, e: &Entry) -> bool { - e.get_header() - .read(&self.header_field_path[..]) - .map(|val| val.map(|v| v == self.expected_value).unwrap_or(false)) - .unwrap_or(false) + self.filter.filter(e) } } 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) } } diff --git a/libimagentryfilter/src/builtin/header/field_gt.rs b/libimagentryfilter/src/builtin/header/field_gt.rs new file mode 100644 index 00000000..6975737b --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_gt.rs @@ -0,0 +1,60 @@ +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; + +use toml::Value; + +struct EqGt { + comp: Value +} + +impl Predicate for EqGt { + + fn evaluate(&self, v: Value) -> bool { + match &self.comp { + &Value::Integer(i) => { + match v { + Value::Integer(j) => i > j, + Value::Float(f) => (i as f64) > f, + _ => false, + } + }, + &Value::Float(f) => { + match v { + Value::Integer(i) => f > (i as f64), + Value::Float(d) => f > d, + _ => false, + } + }, + _ => false, + } + } + +} + +/// Check whether certain header field in a entry is equal to a value +pub struct FieldGt { + filter: FieldPredicate, +} + +impl FieldGt { + + pub fn new(path: FieldPath, expected_value: Value) -> FieldGt { + FieldGt { + filter: FieldPredicate::new(path, Box::new(EqGt { comp: expected_value })), + } + } + +} + +impl Filter for FieldGt { + + fn filter(&self, e: &Entry) -> bool { + self.filter.filter(e) + } + +} + diff --git a/libimagentryfilter/src/builtin/header/field_isempty.rs b/libimagentryfilter/src/builtin/header/field_isempty.rs index a6a5b3d3..a7ff4ab1 100644 --- a/libimagentryfilter/src/builtin/header/field_isempty.rs +++ b/libimagentryfilter/src/builtin/header/field_isempty.rs @@ -30,7 +30,7 @@ impl Filter for FieldIsEmpty { Some(Value::Boolean(_)) => false, Some(Value::Float(_)) => false, Some(Value::Integer(_)) => false, - Some(Value::String(_)) => false, + Some(Value::String(s)) => s.is_empty(), Some(Value::Table(t)) => t.is_empty(), _ => true, } diff --git a/libimagentryfilter/src/builtin/header/field_istype.rs b/libimagentryfilter/src/builtin/header/field_istype.rs index 6ff6d511..8423c0ab 100644 --- a/libimagentryfilter/src/builtin/header/field_istype.rs +++ b/libimagentryfilter/src/builtin/header/field_istype.rs @@ -1,6 +1,8 @@ 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; use toml::Value; @@ -31,17 +33,27 @@ impl Type { } +struct IsTypePred { + ty: Type +} + +impl Predicate for IsTypePred { + + fn evaluate(&self, v: Value) -> bool { + self.ty.matches(&v) + } + +} + pub struct FieldIsType { - header_field_path: FieldPath, - expected_type: Type, + filter: FieldPredicate, } impl FieldIsType { pub fn new(path: FieldPath, expected_type: Type) -> FieldIsType { FieldIsType { - header_field_path: path, - expected_type: expected_type, + filter: FieldPredicate::new(path, Box::new(IsTypePred { ty: expected_type })), } } @@ -50,10 +62,7 @@ impl FieldIsType { impl Filter for FieldIsType { fn filter(&self, e: &Entry) -> bool { - e.get_header() - .read(&self.header_field_path[..]) - .map(|val| val.map(|v| self.expected_type.matches(&v)).unwrap_or(false)) - .unwrap_or(false) + self.filter.filter(e) } } diff --git a/libimagentryfilter/src/builtin/header/field_lt.rs b/libimagentryfilter/src/builtin/header/field_lt.rs new file mode 100644 index 00000000..15558d4b --- /dev/null +++ b/libimagentryfilter/src/builtin/header/field_lt.rs @@ -0,0 +1,60 @@ +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; + +use toml::Value; + +struct EqLt { + comp: Value +} + +impl Predicate for EqLt { + + fn evaluate(&self, v: Value) -> bool { + match &self.comp { + &Value::Integer(i) => { + match v { + Value::Integer(j) => i < j, + Value::Float(f) => (i as f64) < f, + _ => false, + } + }, + &Value::Float(f) => { + match v { + Value::Integer(i) => f < (i as f64), + Value::Float(d) => f < d, + _ => false, + } + }, + _ => false, + } + } + +} + +/// Check whether certain header field in a entry is equal to a value +pub struct FieldLt { + filter: FieldPredicate, +} + +impl FieldLt { + + pub fn new(path: FieldPath, expected_value: Value) -> FieldLt { + FieldLt { + filter: FieldPredicate::new(path, Box::new(EqLt { comp: expected_value })), + } + } + +} + +impl Filter for FieldLt { + + fn filter(&self, e: &Entry) -> bool { + self.filter.filter(e) + } + +} + 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..baece9f6 100644 --- a/libimagentryfilter/src/builtin/header/mod.rs +++ b/libimagentryfilter/src/builtin/header/mod.rs @@ -1,6 +1,9 @@ pub mod field_eq; pub mod field_exists; pub mod field_grep; +pub mod field_gt; pub mod field_isempty; pub mod field_istype; +pub mod field_lt; pub mod field_path; +pub mod field_predicate;