1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::common;
use crate::logs;
use crate::metadata::Level;
use crate::spans;

mod generated {
    #![allow(warnings)]
    #![allow(clippy::all, clippy::pedantic)]
    include!("./generated/rs.devtools.instrument.rs");
}

pub use generated::*;

pub trait Filterable {
    /// Determines if the current item matches the provided filter.
    fn match_filter(&self, metadata: &common::Metadata, filter: &Filter) -> bool;
}

impl Filter {
    /// Checks if a given file name matches the filter's `file` criteria.
    ///
    /// This method will return `true` under two conditions:
    /// - The `file` filter is set (`Some`), and the given file name contains the substring defined in the filter.
    /// - The `file` filter is not set (`None`).
    #[must_use]
    pub fn matches_file(&self, file: &str) -> bool {
        self.file.as_ref().map_or(true, |v| file.contains(v))
    }

    /// Checks if a given text matches the filter's `text` criteria.
    ///
    /// This method will return `true` under two conditions:
    /// - The `text` filter is set (`Some`), and the given text contains the substring defined in the filter.
    /// - The `text` filter is not set (`None`).
    #[must_use]
    pub fn matches_text(&self, text: &str) -> bool {
        self.text.as_ref().map_or(true, |v| text.contains(v))
    }

    /// Checks if a given log level matches the filter's `level` criteria.
    ///
    /// This method will return `true` under two conditions:
    /// - The `level` filter is set (`Some`), and the given log level matches
    ///   the one defined in the filter.
    /// - The `level` filter is not set (`None`).
    #[must_use]
    pub fn matches_level(&self, level: &Level) -> bool {
        self.level
            .and_then(|v| Level::try_from(v).ok())
            .map_or(true, |v| v == *level)
    }
}