devtools_wire_format/
instrument.rs

1use crate::common;
2use crate::logs;
3use crate::metadata::Level;
4use crate::spans;
5
6mod generated {
7    #![allow(warnings)]
8    #![allow(clippy::all, clippy::pedantic)]
9    include!("./generated/rs.devtools.instrument.rs");
10}
11
12pub use generated::*;
13
14pub trait Filterable {
15    /// Determines if the current item matches the provided filter.
16    fn match_filter(&self, metadata: &common::Metadata, filter: &Filter) -> bool;
17}
18
19impl Filter {
20    /// Checks if a given file name matches the filter's `file` criteria.
21    ///
22    /// This method will return `true` under two conditions:
23    /// - The `file` filter is set (`Some`), and the given file name contains the substring defined in the filter.
24    /// - The `file` filter is not set (`None`).
25    #[must_use]
26    pub fn matches_file(&self, file: &str) -> bool {
27        self.file.as_ref().map_or(true, |v| file.contains(v))
28    }
29
30    /// Checks if a given text matches the filter's `text` criteria.
31    ///
32    /// This method will return `true` under two conditions:
33    /// - The `text` filter is set (`Some`), and the given text contains the substring defined in the filter.
34    /// - The `text` filter is not set (`None`).
35    #[must_use]
36    pub fn matches_text(&self, text: &str) -> bool {
37        self.text.as_ref().map_or(true, |v| text.contains(v))
38    }
39
40    /// Checks if a given log level matches the filter's `level` criteria.
41    ///
42    /// This method will return `true` under two conditions:
43    /// - The `level` filter is set (`Some`), and the given log level matches
44    ///   the one defined in the filter.
45    /// - The `level` filter is not set (`None`).
46    #[must_use]
47    pub fn matches_level(&self, level: &Level) -> bool {
48        self.level
49            .and_then(|v| Level::try_from(v).ok())
50            .map_or(true, |v| v == *level)
51    }
52}