Add filter: header::field_grep::FieldGrep

This commit is contained in:
Matthias Beyer 2016-02-02 17:21:08 +01:00
parent 0dc88877c6
commit 41564a7d8e
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,43 @@
use regex::Regex;
use toml::Value;
use libimagstore::store::Entry;
use builtin::header::field_path::FieldPath;
use filter::Filter;
/// Check whether certain header field in a entry is equal to a value
pub struct FieldGrep {
header_field_path: FieldPath,
grep: Regex,
}
impl FieldGrep {
pub fn new(path: FieldPath, grep: Regex) -> FieldGrep {
FieldGrep {
header_field_path: path,
grep: grep,
}
}
}
impl Filter for FieldGrep {
fn filter(&self, e: &Entry) -> bool {
let header = e.get_header();
self.header_field_path
.walk(header)
.map(|v| {
match v {
Value::String(s) => self.grep.captures(&s[..]).is_some(),
_ => false,
}
})
.unwrap_or(false)
}
}

View File

@ -1,3 +1,4 @@
pub mod field_eq;
pub mod field_grep;
pub mod field_path;