diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-02-03 16:33:01 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-02-07 01:45:09 +0100 |
commit | f81190fb8ae92ca27f25aca2fc97abf5d2f25d29 (patch) | |
tree | f2211190dc43d5bab62ca547bde82950fb7c67fb /libimagentryfilter | |
parent | bcf8cf0447084f9ddb94312891cabc6fcce16c0d (diff) |
Add builtin content filter: grep
Diffstat (limited to 'libimagentryfilter')
-rw-r--r-- | libimagentryfilter/src/builtin/content/grep.rs | 57 | ||||
-rw-r--r-- | libimagentryfilter/src/builtin/content/mod.rs | 1 |
2 files changed, 58 insertions, 0 deletions
diff --git a/libimagentryfilter/src/builtin/content/grep.rs b/libimagentryfilter/src/builtin/content/grep.rs new file mode 100644 index 00000000..a3d89af4 --- /dev/null +++ b/libimagentryfilter/src/builtin/content/grep.rs @@ -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() + } + +} + diff --git a/libimagentryfilter/src/builtin/content/mod.rs b/libimagentryfilter/src/builtin/content/mod.rs index e69de29b..21e9e600 100644 --- a/libimagentryfilter/src/builtin/content/mod.rs +++ b/libimagentryfilter/src/builtin/content/mod.rs @@ -0,0 +1 @@ +pub mod grep; |