env_filter/
op.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub(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")]
12impl FilterOp {
13    pub(crate) fn new(spec: &str) -> Result<Self, String> {
14        match regex::Regex::new(spec) {
15            Ok(r) => Ok(Self { inner: r }),
16            Err(e) => Err(e.to_string()),
17        }
18    }
19
20    pub(crate) fn is_match(&self, s: &str) -> bool {
21        self.inner.is_match(s)
22    }
23}
24
25#[cfg(not(feature = "regex"))]
26impl 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
38impl fmt::Display for FilterOp {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        self.inner.fmt(f)
41    }
42}