Add field_predicate filter
This commit is contained in:
parent
9a918c9252
commit
4de014c41a
2 changed files with 46 additions and 0 deletions
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,3 +4,4 @@ pub mod field_grep;
|
||||||
pub mod field_isempty;
|
pub mod field_isempty;
|
||||||
pub mod field_istype;
|
pub mod field_istype;
|
||||||
pub mod field_path;
|
pub mod field_path;
|
||||||
|
pub mod field_predicate;
|
||||||
|
|
Loading…
Reference in a new issue