Reimplement FieldEq with FieldPredicate
This commit is contained in:
parent
c76d654f9b
commit
f86a8968a4
1 changed files with 17 additions and 8 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue