Skip to main content

env_filter/
op.rs

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