Merge pull request #246 from matthiasbeyer/libimagentryfilter/more-filters

Libimagentryfilter/more filters
This commit is contained in:
Matthias Beyer 2016-03-18 10:28:26 +01:00
commit 926dffe8b7
8 changed files with 223 additions and 30 deletions

View file

@ -1,22 +1,34 @@
use libimagstore::store::Entry; 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::Predicate;
use filter::Filter; use filter::Filter;
use toml::Value; 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 /// Check whether certain header field in a entry is equal to a value
pub struct FieldEq { pub struct FieldEq {
header_field_path: FieldPath, filter: FieldPredicate<EqPred>,
expected_value: Value
} }
impl FieldEq { impl FieldEq {
pub fn new(path: FieldPath, expected_value: Value) -> FieldEq { pub fn new(path: FieldPath, expected_value: Value) -> FieldEq {
FieldEq { FieldEq {
header_field_path: path, filter: FieldPredicate::new(path, Box::new(EqPred { expected: expected_value })),
expected_value: expected_value,
} }
} }
@ -25,10 +37,7 @@ impl FieldEq {
impl Filter for FieldEq { impl Filter for FieldEq {
fn filter(&self, e: &Entry) -> bool { fn filter(&self, e: &Entry) -> bool {
e.get_header() self.filter.filter(e)
.read(&self.header_field_path[..])
.map(|val| val.map(|v| v == self.expected_value).unwrap_or(false))
.unwrap_or(false)
} }
} }

View file

@ -4,20 +4,35 @@ use toml::Value;
use libimagstore::store::Entry; 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::Predicate;
use filter::Filter; 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 /// Check whether certain header field in a entry is equal to a value
pub struct FieldGrep { pub struct FieldGrep {
header_field_path: FieldPath, filter: FieldPredicate<EqGrep>,
grep: Regex,
} }
impl FieldGrep { impl FieldGrep {
pub fn new(path: FieldPath, grep: Regex) -> FieldGrep { pub fn new(path: FieldPath, grep: Regex) -> FieldGrep {
FieldGrep { FieldGrep {
header_field_path: path, filter: FieldPredicate::new(path, Box::new(EqGrep { regex: grep})),
grep: grep,
} }
} }
@ -26,15 +41,7 @@ impl FieldGrep {
impl Filter for FieldGrep { impl Filter for FieldGrep {
fn filter(&self, e: &Entry) -> bool { fn filter(&self, e: &Entry) -> bool {
e.get_header() self.filter.filter(e)
.read(&self.header_field_path[..])
.map(|v| {
match v {
Some(Value::String(s)) => self.grep.captures(&s[..]).is_some(),
_ => false,
}
})
.unwrap_or(false)
} }
} }

View file

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

View file

@ -30,7 +30,7 @@ impl Filter for FieldIsEmpty {
Some(Value::Boolean(_)) => false, Some(Value::Boolean(_)) => false,
Some(Value::Float(_)) => false, Some(Value::Float(_)) => false,
Some(Value::Integer(_)) => false, Some(Value::Integer(_)) => false,
Some(Value::String(_)) => false, Some(Value::String(s)) => s.is_empty(),
Some(Value::Table(t)) => t.is_empty(), Some(Value::Table(t)) => t.is_empty(),
_ => true, _ => true,
} }

View file

@ -1,6 +1,8 @@
use libimagstore::store::Entry; 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::Predicate;
use filter::Filter; use filter::Filter;
use toml::Value; 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 { pub struct FieldIsType {
header_field_path: FieldPath, filter: FieldPredicate<IsTypePred>,
expected_type: Type,
} }
impl FieldIsType { impl FieldIsType {
pub fn new(path: FieldPath, expected_type: Type) -> FieldIsType { pub fn new(path: FieldPath, expected_type: Type) -> FieldIsType {
FieldIsType { FieldIsType {
header_field_path: path, filter: FieldPredicate::new(path, Box::new(IsTypePred { ty: expected_type })),
expected_type: expected_type,
} }
} }
@ -50,10 +62,7 @@ impl FieldIsType {
impl Filter for FieldIsType { impl Filter for FieldIsType {
fn filter(&self, e: &Entry) -> bool { fn filter(&self, e: &Entry) -> bool {
e.get_header() self.filter.filter(e)
.read(&self.header_field_path[..])
.map(|val| val.map(|v| self.expected_type.matches(&v)).unwrap_or(false))
.unwrap_or(false)
} }
} }

View file

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

View file

@ -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<P: Predicate> {
header_field_path: FieldPath,
predicate: Box<P>,
}
impl<P: Predicate> FieldPredicate<P> {
pub fn new(path: FieldPath, predicate: Box<P>) -> FieldPredicate<P> {
FieldPredicate {
header_field_path: path,
predicate: predicate,
}
}
}
impl<P: Predicate> Filter for FieldPredicate<P> {
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)
}
}

View file

@ -1,6 +1,9 @@
pub mod field_eq; pub mod field_eq;
pub mod field_exists; pub mod field_exists;
pub mod field_grep; pub mod field_grep;
pub mod field_gt;
pub mod field_isempty; pub mod field_isempty;
pub mod field_istype; pub mod field_istype;
pub mod field_lt;
pub mod field_path; pub mod field_path;
pub mod field_predicate;