Add field_grep filter
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
564d8d84fe
commit
0a94160895
1 changed files with 20 additions and 13 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue