1
- use std:: collections:: HashMap ;
1
+ use std:: { collections:: HashMap , str :: FromStr } ;
2
2
3
3
/// Transforms for classifying (tagging and categorizing) events.
4
4
///
@@ -8,6 +8,7 @@ use fancy_regex::Regex;
8
8
9
9
pub enum Rule {
10
10
None ,
11
+ Logical ( LogicalRule ) ,
11
12
Regex ( RegexRule ) ,
12
13
KeyValue ( KeyValueRule ) ,
13
14
}
@@ -16,6 +17,7 @@ impl RuleTrait for Rule {
16
17
fn matches ( & self , event : & Event ) -> bool {
17
18
match self {
18
19
Rule :: None => false ,
20
+ Rule :: Logical ( rule) => rule. matches ( event) ,
19
21
Rule :: Regex ( rule) => rule. matches ( event) ,
20
22
Rule :: KeyValue ( rule) => rule. matches ( event) ,
21
23
}
@@ -92,6 +94,45 @@ impl RuleTrait for KeyValueRule {
92
94
}
93
95
}
94
96
97
+ pub enum LogicalOperator {
98
+ Or ,
99
+ And ,
100
+ }
101
+
102
+ impl FromStr for LogicalOperator {
103
+ type Err = String ;
104
+
105
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
106
+ match s {
107
+ "or" => Ok ( Self :: Or ) ,
108
+ "and" => Ok ( Self :: And ) ,
109
+ _ => Err ( format ! ( "Invalid logical operator: {}" , s) ) ,
110
+ }
111
+ }
112
+ }
113
+
114
+ pub struct LogicalRule {
115
+ rules : Vec < Rule > ,
116
+ operator : LogicalOperator ,
117
+ }
118
+
119
+ impl LogicalRule {
120
+ pub fn new ( rules : Vec < Rule > , operator : LogicalOperator ) -> Self {
121
+ Self { rules, operator }
122
+ }
123
+ }
124
+
125
+ impl RuleTrait for LogicalRule {
126
+ fn matches ( & self , event : & Event ) -> bool {
127
+ use LogicalOperator :: { And , Or } ;
128
+
129
+ match self . operator {
130
+ Or => self . rules . iter ( ) . any ( |rule| rule. matches ( event) ) ,
131
+ And => self . rules . iter ( ) . all ( |rule| rule. matches ( event) ) ,
132
+ }
133
+ }
134
+ }
135
+
95
136
/// Categorizes a list of events
96
137
///
97
138
/// An event can only have one category, although the category may have a hierarchy,
0 commit comments