log_analyzer/models/
filter.rs

1use super::log_line::LogLine;
2
3use regex::Regex;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Clone, Copy, Eq, PartialEq, Debug)]
7/// Describe the action of a filter
8pub enum FilterAction {
9    /// Just add a color marker
10    MARKER,
11    /// Exclude what is not matched by this filter
12    INCLUDE,
13    /// Exclude what is matched by this filter
14    EXCLUDE,
15}
16
17impl From<usize> for FilterAction {
18    fn from(v: usize) -> Self {
19        match v {
20            0 => FilterAction::INCLUDE,
21            1 => FilterAction::EXCLUDE,
22            _ => FilterAction::MARKER,
23        }
24    }
25}
26
27impl Into<usize> for FilterAction {
28    fn into(self) -> usize {
29        match self {
30            FilterAction::INCLUDE => 0,
31            FilterAction::EXCLUDE => 1,
32            FilterAction::MARKER => 2,
33        }
34    }
35}
36
37impl Default for FilterAction {
38    fn default() -> Self {
39        FilterAction::MARKER
40    }
41}
42
43
44#[derive(Default, Clone, Debug)]
45/// Struct with cached vector of log_line keys with their associated regex
46pub struct LogFilter {
47    pub action: FilterAction,
48    /// List of (log_line_key, regex)
49    pub filters: Vec<(String, Regex)>,
50    /// Color - if any
51    pub color: Option<(u8, u8, u8)>
52}
53
54impl From<Filter> for LogFilter {
55    fn from(f: Filter) -> Self {
56        Self { action: f.action, filters: f.get_filters(), color: f.filter.color }
57    }
58}
59
60
61
62#[derive(Default, Serialize, Deserialize, Debug)]
63/// Base filter definition.
64pub struct Filter {
65    pub alias: String,
66    pub action: FilterAction,
67    /// Contains the regex filtering in the `LogLine` fields
68    pub filter: LogLine
69}
70
71impl Filter {
72    /// Get the valid filters from the filter data
73    /// Returns a vector of (Key, Regex); Key is to be used with the get method of LogLines
74    pub fn get_filters(&self) -> Vec<(String, Regex)> {
75        let mut filters = Vec::new();
76        for (k, v) in self.filter.values() {
77            if let Ok(re) = Regex::new(v) {
78                filters.push((k.into(), re))
79            }
80        }
81
82        filters
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn serialize() {
92        let filter = Filter {
93            alias: "All".into(),
94            action: FilterAction::MARKER,
95            filter: LogLine {
96                index: "0".to_string(),
97                ..Default::default()
98            },
99        };
100        let json = serde_json::to_string(&filter);
101        assert!(json.is_ok())
102    }
103
104    #[test]
105    fn deserialize() {
106        let json = r#"
107        {
108            "alias": "Name",
109            "action": "INCLUDE",
110            "filter": {"payload": ".*"}
111        }"#;
112
113        let filter: Result<Filter, serde_json::Error> = serde_json::from_str(json);
114        assert!(filter.is_ok())
115    }
116
117    #[test]
118    fn deserialize_list() {
119        let json = r#"[
120            {
121                "alias": "Name",
122                "action": "INCLUDE",
123                "filter": {"payload": ".*"}
124            },
125            {
126                "alias": "All",
127                "action": "EXCLUDE",
128                "filter": {"payload": ".*"}
129            }
130        ]"#;
131
132        let filter: Result<Vec<Filter>, serde_json::Error> = serde_json::from_str(json);
133        assert!(filter.is_ok())
134    }
135}