Add field_grep filter

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Kai Sickeler 2016-03-12 17:39:57 +01:00 committed by Matthias Beyer
parent 564d8d84fe
commit 0a94160895

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)
} }
} }