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_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)
}

View file

@ -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())
}
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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))
}
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
)
}
}