Add builtin content filter: grep
This commit is contained in:
parent
bcf8cf0447
commit
f81190fb8a
2 changed files with 58 additions and 0 deletions
57
libimagentryfilter/src/builtin/content/grep.rs
Normal file
57
libimagentryfilter/src/builtin/content/grep.rs
Normal file
|
@ -0,0 +1,57 @@
|
|||
use std::convert::Into;
|
||||
|
||||
use regex::Regex;
|
||||
use regex::Error as RError;
|
||||
|
||||
use libimagstore::store::Entry;
|
||||
|
||||
use builtin::header::field_path::FieldPath;
|
||||
use filter::Filter;
|
||||
|
||||
pub trait IntoRegex {
|
||||
|
||||
fn into_regex(self) -> Result<Regex, RError>;
|
||||
|
||||
}
|
||||
|
||||
impl<'a> IntoRegex for &'a str {
|
||||
|
||||
fn into_regex(self) -> Result<Regex, RError> {
|
||||
Regex::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoRegex for Regex {
|
||||
|
||||
fn into_regex(self) -> Result<Regex, RError> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ContentGrep {
|
||||
regex: Regex,
|
||||
}
|
||||
|
||||
impl ContentGrep {
|
||||
|
||||
pub fn new<IR>(regex: IR) -> Result<ContentGrep, RError>
|
||||
where IR: IntoRegex
|
||||
{
|
||||
regex.into_regex()
|
||||
.map(|reg| {
|
||||
ContentGrep {
|
||||
regex: reg,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Filter for ContentGrep {
|
||||
|
||||
fn filter(&self, e: &Entry) -> bool {
|
||||
self.regex.captures(&e.get_content()[..]).is_some()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
pub mod grep;
|
Loading…
Reference in a new issue