From f81190fb8ae92ca27f25aca2fc97abf5d2f25d29 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 3 Feb 2016 16:33:01 +0100 Subject: [PATCH] Add builtin content filter: grep --- .../src/builtin/content/grep.rs | 57 +++++++++++++++++++ libimagentryfilter/src/builtin/content/mod.rs | 1 + 2 files changed, 58 insertions(+) create mode 100644 libimagentryfilter/src/builtin/content/grep.rs 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; + +} + +impl<'a> IntoRegex for &'a str { + + fn into_regex(self) -> Result { + Regex::new(self) + } +} + +impl<'a> IntoRegex for Regex { + + fn into_regex(self) -> Result { + Ok(self) + } +} + +pub struct ContentGrep { + regex: Regex, +} + +impl ContentGrep { + + pub fn new(regex: IR) -> Result + 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;