Merge pull request #246 from matthiasbeyer/libimagentryfilter/more-filters
Libimagentryfilter/more filters
This commit is contained in:
commit
926dffe8b7
8 changed files with 223 additions and 30 deletions
|
@ -1,22 +1,34 @@
|
|||
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 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
|
||||
pub struct FieldEq {
|
||||
header_field_path: FieldPath,
|
||||
expected_value: Value
|
||||
filter: FieldPredicate<EqPred>,
|
||||
}
|
||||
|
||||
impl FieldEq {
|
||||
|
||||
pub fn new(path: FieldPath, expected_value: Value) -> FieldEq {
|
||||
FieldEq {
|
||||
header_field_path: path,
|
||||
expected_value: expected_value,
|
||||
filter: FieldPredicate::new(path, Box::new(EqPred { expected: expected_value })),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,10 +37,7 @@ impl FieldEq {
|
|||
impl Filter for FieldEq {
|
||||
|
||||
fn filter(&self, e: &Entry) -> bool {
|
||||
e.get_header()
|
||||
.read(&self.header_field_path[..])
|
||||
.map(|val| val.map(|v| v == self.expected_value).unwrap_or(false))
|
||||
.unwrap_or(false)
|
||||
self.filter.filter(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,20 +4,35 @@ use toml::Value;
|
|||
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;
|
||||
|
||||
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
|
||||
pub struct FieldGrep {
|
||||
header_field_path: FieldPath,
|
||||
grep: Regex,
|
||||
filter: FieldPredicate<EqGrep>,
|
||||
}
|
||||
|
||||
impl FieldGrep {
|
||||
|
||||
pub fn new(path: FieldPath, grep: Regex) -> FieldGrep {
|
||||
FieldGrep {
|
||||
header_field_path: path,
|
||||
grep: grep,
|
||||
filter: FieldPredicate::new(path, Box::new(EqGrep { regex: grep})),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,15 +41,7 @@ impl FieldGrep {
|
|||
impl Filter for FieldGrep {
|
||||
|
||||
fn filter(&self, e: &Entry) -> bool {
|
||||
e.get_header()
|
||||
.read(&self.header_field_path[..])
|
||||
.map(|v| {
|
||||
match v {
|
||||
Some(Value::String(s)) => self.grep.captures(&s[..]).is_some(),
|
||||
_ => false,
|
||||
}
|
||||
})
|
||||
.unwrap_or(false)
|
||||
self.filter.filter(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
60
libimagentryfilter/src/builtin/header/field_gt.rs
Normal file
60
libimagentryfilter/src/builtin/header/field_gt.rs
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ impl Filter for FieldIsEmpty {
|
|||
Some(Value::Boolean(_)) => false,
|
||||
Some(Value::Float(_)) => false,
|
||||
Some(Value::Integer(_)) => false,
|
||||
Some(Value::String(_)) => false,
|
||||
Some(Value::String(s)) => s.is_empty(),
|
||||
Some(Value::Table(t)) => t.is_empty(),
|
||||
_ => true,
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
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;
|
||||
|
@ -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 {
|
||||
header_field_path: FieldPath,
|
||||
expected_type: Type,
|
||||
filter: FieldPredicate<IsTypePred>,
|
||||
}
|
||||
|
||||
impl FieldIsType {
|
||||
|
||||
pub fn new(path: FieldPath, expected_type: Type) -> FieldIsType {
|
||||
FieldIsType {
|
||||
header_field_path: path,
|
||||
expected_type: expected_type,
|
||||
filter: FieldPredicate::new(path, Box::new(IsTypePred { ty: expected_type })),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,10 +62,7 @@ impl FieldIsType {
|
|||
impl Filter for FieldIsType {
|
||||
|
||||
fn filter(&self, e: &Entry) -> bool {
|
||||
e.get_header()
|
||||
.read(&self.header_field_path[..])
|
||||
.map(|val| val.map(|v| self.expected_type.matches(&v)).unwrap_or(false))
|
||||
.unwrap_or(false)
|
||||
self.filter.filter(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
60
libimagentryfilter/src/builtin/header/field_lt.rs
Normal file
60
libimagentryfilter/src/builtin/header/field_lt.rs
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
45
libimagentryfilter/src/builtin/header/field_predicate.rs
Normal file
45
libimagentryfilter/src/builtin/header/field_predicate.rs
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
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_lt;
|
||||
pub mod field_path;
|
||||
pub mod field_predicate;
|
||||
|
|
Loading…
Reference in a new issue