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 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue