From 7b9f8dc8adee3d46c297b9368dfa0daafa8d17d2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 10 Mar 2016 20:36:50 +0100 Subject: [PATCH] Add field_gt filter --- .../src/builtin/header/field_gt.rs | 60 +++++++++++++++++++ libimagentryfilter/src/builtin/header/mod.rs | 1 + 2 files changed, 61 insertions(+) create mode 100644 libimagentryfilter/src/builtin/header/field_gt.rs 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/mod.rs b/libimagentryfilter/src/builtin/header/mod.rs index c3725738..de5a3327 100644 --- a/libimagentryfilter/src/builtin/header/mod.rs +++ b/libimagentryfilter/src/builtin/header/mod.rs @@ -1,6 +1,7 @@ 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_path;