diff options
8 files changed, 73 insertions, 45 deletions
diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_eq.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_eq.rs index 9736057f..9562dddd 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_eq.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_eq.rs @@ -22,7 +22,9 @@ use libimagstore::store::Entry; use builtin::header::field_path::FieldPath; use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::Predicate; -use filters::filter::Filter; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; use toml::Value; @@ -53,9 +55,10 @@ impl FieldEq { } -impl Filter<Entry> for FieldEq { +impl FailableFilter<Entry> for FieldEq { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { + fn filter(&self, e: &Entry) -> Result<bool> { self.filter.filter(e) } diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_exists.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_exists.rs index 782043a0..76428874 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_exists.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_exists.rs @@ -20,7 +20,9 @@ use libimagstore::store::Entry; use toml_query::read::TomlValueReadExt; -use filters::filter::Filter; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; use builtin::header::field_path::FieldPath; @@ -38,10 +40,11 @@ impl FieldExists { } -impl Filter<Entry> for FieldExists { +impl FailableFilter<Entry> for FieldExists { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { - e.get_header().read(&self.header_field_path[..]).is_ok() + fn filter(&self, e: &Entry) -> Result<bool> { + e.get_header().read(&self.header_field_path[..]).map_err(FE::from).map(|o| o.is_some()) } } diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_grep.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_grep.rs index 7d47cf23..5367eee8 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_grep.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_grep.rs @@ -25,7 +25,9 @@ use libimagstore::store::Entry; use builtin::header::field_path::FieldPath; use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::Predicate; -use filters::filter::Filter; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; struct EqGrep{ regex: Regex @@ -57,9 +59,10 @@ impl FieldGrep { } -impl Filter<Entry> for FieldGrep { +impl FailableFilter<Entry> for FieldGrep { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { + fn filter(&self, e: &Entry) -> Result<bool> { self.filter.filter(e) } diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_gt.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_gt.rs index 30fc93a7..b7e629bc 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_gt.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_gt.rs @@ -22,7 +22,9 @@ use libimagstore::store::Entry; use builtin::header::field_path::FieldPath; use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::Predicate; -use filters::filter::Filter; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; use toml::Value; @@ -69,9 +71,10 @@ impl FieldGt { } -impl Filter<Entry> for FieldGt { +impl FailableFilter<Entry> for FieldGt { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { + fn filter(&self, e: &Entry) -> Result<bool> { self.filter.filter(e) } diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_isempty.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_isempty.rs index 41a4d4c7..d1c4721e 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_isempty.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_isempty.rs @@ -21,7 +21,9 @@ use libimagstore::store::Entry; use toml_query::read::TomlValueReadExt; use builtin::header::field_path::FieldPath; -use filters::filter::Filter; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; use toml::Value; @@ -39,23 +41,25 @@ impl FieldIsEmpty { } -impl Filter<Entry> for FieldIsEmpty { +impl FailableFilter<Entry> for FieldIsEmpty { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { - e.get_header() - .read(&self.header_field_path[..]) - .map(|v| { - match v { - Some(&Value::Array(ref a)) => a.is_empty(), - Some(&Value::String(ref s)) => s.is_empty(), - Some(&Value::Table(ref t)) => t.is_empty(), - Some(&Value::Boolean(_)) | - Some(&Value::Float(_)) | - Some(&Value::Integer(_)) => false, - _ => true, - } - }) - .unwrap_or(false) + fn filter(&self, e: &Entry) -> Result<bool> { + Ok(e + .get_header() + .read(&self.header_field_path[..])? + .map(|v| { + match v { + &Value::Array(ref a) => a.is_empty(), + &Value::String(ref s) => s.is_empty(), + &Value::Table(ref t) => t.is_empty(), + &Value::Boolean(_) | + &Value::Float(_) | + &Value::Datetime(_) | + &Value::Integer(_) => false, + } + }) + .unwrap_or(true)) } } diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_istype.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_istype.rs index 453aaef2..a0968d37 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_istype.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_istype.rs @@ -22,7 +22,9 @@ use libimagstore::store::Entry; use builtin::header::field_path::FieldPath; use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::Predicate; -use filters::filter::Filter; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; use toml::Value; @@ -78,9 +80,10 @@ impl FieldIsType { } -impl Filter<Entry> for FieldIsType { +impl FailableFilter<Entry> for FieldIsType { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { + fn filter(&self, e: &Entry) -> Result<bool> { self.filter.filter(e) } diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_lt.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_lt.rs index 3123621a..c9b89fec 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_lt.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_lt.rs @@ -22,7 +22,9 @@ use libimagstore::store::Entry; use builtin::header::field_path::FieldPath; use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::Predicate; -use filters::filter::Filter; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; use toml::Value; @@ -69,9 +71,10 @@ impl FieldLt { } -impl Filter<Entry> for FieldLt { +impl FailableFilter<Entry> for FieldLt { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { + fn filter(&self, e: &Entry) -> Result<bool> { self.filter.filter(e) } diff --git a/lib/entry/libimagentryfilter/src/builtin/header/field_predicate.rs b/lib/entry/libimagentryfilter/src/builtin/header/field_predicate.rs index ef27639b..5a7dbb9b 100644 --- a/lib/entry/libimagentryfilter/src/builtin/header/field_predicate.rs +++ b/lib/entry/libimagentryfilter/src/builtin/header/field_predicate.rs @@ -20,17 +20,22 @@ use libimagstore::store::Entry; use toml_query::read::TomlValueReadExt; +use toml::Value; use builtin::header::field_path::FieldPath; -use filters::filter::Filter; - -use toml::Value; +use filters::failable::filter::FailableFilter; +use error::Result; +use error::FilterError as FE; pub trait Predicate { fn evaluate(&self, &Value) -> bool; } /// Check whether certain header field in a entry is equal to a value +/// +/// # Notice +/// +/// Returns false if the value is not present. pub struct FieldPredicate<P: Predicate> { header_field_path: FieldPath, predicate: Box<P>, @@ -47,15 +52,16 @@ impl<P: Predicate> FieldPredicate<P> { } -impl<P: Predicate> Filter<Entry> for FieldPredicate<P> { +impl<P: Predicate> FailableFilter<Entry> for FieldPredicate<P> { + type Error = FE; - fn filter(&self, e: &Entry) -> bool { - e.get_header() - .read(&self.header_field_path[..]) - .map(|val| val.map(|v| (*self.predicate).evaluate(v)).unwrap_or(false)) + fn filter(&self, e: &Entry) -> Result<bool> { + Ok(e.get_header() + .read(&self.header_field_path[..])? + .map(|v| (*self.predicate).evaluate(v)) .unwrap_or(false) + ) } } - |