Move header filters to FailableFilter

This commit is contained in:
Matthias Beyer 2018-04-29 11:07:45 +02:00
parent 30ac77d626
commit fc92cfc36f
8 changed files with 73 additions and 45 deletions

View file

@ -22,7 +22,9 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath; use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate; 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; 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) self.filter.filter(e)
} }

View file

@ -20,7 +20,9 @@
use libimagstore::store::Entry; use libimagstore::store::Entry;
use toml_query::read::TomlValueReadExt; 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; 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 { fn filter(&self, e: &Entry) -> Result<bool> {
e.get_header().read(&self.header_field_path[..]).is_ok() e.get_header().read(&self.header_field_path[..]).map_err(FE::from).map(|o| o.is_some())
} }
} }

View file

@ -25,7 +25,9 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath; use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate; 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{ struct EqGrep{
regex: Regex 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) self.filter.filter(e)
} }

View file

@ -22,7 +22,9 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath; use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate; 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; 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) self.filter.filter(e)
} }

View file

@ -21,7 +21,9 @@ use libimagstore::store::Entry;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use builtin::header::field_path::FieldPath; 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; 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 { fn filter(&self, e: &Entry) -> Result<bool> {
e.get_header() Ok(e
.read(&self.header_field_path[..]) .get_header()
.map(|v| { .read(&self.header_field_path[..])?
match v { .map(|v| {
Some(&Value::Array(ref a)) => a.is_empty(), match v {
Some(&Value::String(ref s)) => s.is_empty(), &Value::Array(ref a) => a.is_empty(),
Some(&Value::Table(ref t)) => t.is_empty(), &Value::String(ref s) => s.is_empty(),
Some(&Value::Boolean(_)) | &Value::Table(ref t) => t.is_empty(),
Some(&Value::Float(_)) | &Value::Boolean(_) |
Some(&Value::Integer(_)) => false, &Value::Float(_) |
_ => true, &Value::Datetime(_) |
} &Value::Integer(_) => false,
}) }
.unwrap_or(false) })
.unwrap_or(true))
} }
} }

View file

@ -22,7 +22,9 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath; use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate; 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; 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) self.filter.filter(e)
} }

View file

@ -22,7 +22,9 @@ use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath; use builtin::header::field_path::FieldPath;
use builtin::header::field_predicate::FieldPredicate; use builtin::header::field_predicate::FieldPredicate;
use builtin::header::field_predicate::Predicate; 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; 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) self.filter.filter(e)
} }

View file

@ -20,17 +20,22 @@
use libimagstore::store::Entry; use libimagstore::store::Entry;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use toml::Value;
use builtin::header::field_path::FieldPath; use builtin::header::field_path::FieldPath;
use filters::filter::Filter; use filters::failable::filter::FailableFilter;
use error::Result;
use toml::Value; use error::FilterError as FE;
pub trait Predicate { pub trait Predicate {
fn evaluate(&self, &Value) -> bool; fn evaluate(&self, &Value) -> bool;
} }
/// Check whether certain header field in a entry is equal to a value /// 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> { pub struct FieldPredicate<P: Predicate> {
header_field_path: FieldPath, header_field_path: FieldPath,
predicate: Box<P>, 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 { fn filter(&self, e: &Entry) -> Result<bool> {
e.get_header() Ok(e.get_header()
.read(&self.header_field_path[..]) .read(&self.header_field_path[..])?
.map(|val| val.map(|v| (*self.predicate).evaluate(v)).unwrap_or(false)) .map(|v| (*self.predicate).evaluate(v))
.unwrap_or(false) .unwrap_or(false)
)
} }
} }