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