1 | use std::fmt; |
2 | |
3 | #[derive (Debug)] |
4 | pub(crate) struct FilterOp { |
5 | #[cfg (feature = "regex" )] |
6 | inner: regex::Regex, |
7 | #[cfg (not(feature = "regex" ))] |
8 | inner: String, |
9 | } |
10 | |
11 | #[cfg (feature = "regex" )] |
12 | impl FilterOp { |
13 | pub(crate) fn new(spec: &str) -> Result<Self, String> { |
14 | match regex::Regex::new(re:spec) { |
15 | Ok(r: Regex) => Ok(Self { inner: r }), |
16 | Err(e: Error) => Err(e.to_string()), |
17 | } |
18 | } |
19 | |
20 | pub(crate) fn is_match(&self, s: &str) -> bool { |
21 | self.inner.is_match(haystack:s) |
22 | } |
23 | } |
24 | |
25 | #[cfg (not(feature = "regex" ))] |
26 | impl FilterOp { |
27 | pub fn new(spec: &str) -> Result<Self, String> { |
28 | Ok(Self { |
29 | inner: spec.to_string(), |
30 | }) |
31 | } |
32 | |
33 | pub fn is_match(&self, s: &str) -> bool { |
34 | s.contains(&self.inner) |
35 | } |
36 | } |
37 | |
38 | impl fmt::Display for FilterOp { |
39 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
40 | self.inner.fmt(f) |
41 | } |
42 | } |
43 | |