1- use std:: collections:: HashMap ;
1+ use std:: { collections:: HashMap , str :: FromStr } ;
22
33/// Transforms for classifying (tagging and categorizing) events.
44///
@@ -8,6 +8,7 @@ use fancy_regex::Regex;
88
99pub enum Rule {
1010 None ,
11+ Logical ( LogicalRule ) ,
1112 Regex ( RegexRule ) ,
1213 KeyValue ( KeyValueRule ) ,
1314}
@@ -16,6 +17,7 @@ impl RuleTrait for Rule {
1617 fn matches ( & self , event : & Event ) -> bool {
1718 match self {
1819 Rule :: None => false ,
20+ Rule :: Logical ( rule) => rule. matches ( event) ,
1921 Rule :: Regex ( rule) => rule. matches ( event) ,
2022 Rule :: KeyValue ( rule) => rule. matches ( event) ,
2123 }
@@ -92,6 +94,45 @@ impl RuleTrait for KeyValueRule {
9294 }
9395}
9496
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+
95136/// Categorizes a list of events
96137///
97138/// An event can only have one category, although the category may have a hierarchy,
0 commit comments