gene 0.7.2

Crate providing a log matching framework written in Rust
Documentation
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
//! Rule definitions and compilation for the Gene event scanning engine.
//!
//! This module provides the core types and functionality for defining, compiling,
//! and executing rules that match and filter events. It includes:
//!
//! - Rule definitions with match expressions and conditions
//! - Decision system for include/exclude logic
//! - Rule types (detection, filter, dependency)
//! - Metadata and parameter support
//! - Compilation pipeline for rule optimization
//!
//! # Key Types
//!
//! - [`Rule`]: Source rule definition in YAML format
//! - [`CompiledRule`]: Optimized, executable (by the engine) rule representation
//! - [`Decision`]: Include/exclude decision enum
//! - [`Type`]: Rule type enum (detection, filter, dependency)
//! - [`enum@Error`]: Rule compilation and processing errors

use self::{attack::AttackId, condition::Condition, matcher::Match};
use crate::{map::deserialize_uk_hashmap, template::Templates, Event};

use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{
    borrow::Cow,
    collections::{HashMap, HashSet},
    io,
    str::FromStr,
};
use thiserror::Error;

// publish modules
mod condition;
// used to parse path
pub(crate) mod matcher;

/// Maximum severity value for rules.
///
/// This constant defines the upper bound for rule severity values in the engine.
/// Severity values are used to prioritize and categorize detected events, with
/// higher values indicating more severe or important detections.
///
/// The severity scale ranges from 0 to this maximum value (inclusive). When multiple
/// rules match an event, their severity values are summed but bounded by this maximum.
pub const MAX_SEVERITY: u8 = 10;

pub(crate) fn bound_severity(sev: u8) -> u8 {
    core::cmp::min(sev, MAX_SEVERITY)
}

lazy_static! {
    static ref ATTACK_ID_RE: Regex = Regex::new(r"^[A-Za-z]+[0-9]+(\.[0-9]+)?$").unwrap();
}

mod attack {
    use thiserror::Error;

    use super::ATTACK_ID_RE;

    #[derive(Debug, Error)]
    pub enum Error {
        #[error("invalid attack id: {0}")]
        Invalid(String),
    }

    #[derive(Hash)]
    pub(crate) struct AttackId(String);

    impl AttackId {
        pub(crate) fn parse<S: AsRef<str>>(s: S) -> Result<Self, Error> {
            let s = s.as_ref();
            if !ATTACK_ID_RE.is_match(s) {
                return Err(Error::Invalid(s.into()));
            }
            Ok(Self(s.to_uppercase()))
        }
    }

    impl From<AttackId> for String {
        fn from(value: AttackId) -> Self {
            value.0
        }
    }
}

/// Represents the type of [`Rule`]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Type {
    /// Use it to encode detection information.
    /// Rule will be used to update [crate::ScanResult]
    Detection,
    /// Use this type for rules to filter in/out
    /// events. Only `actions` section of the rule will
    /// be used to update [crate::ScanResult].
    Filter,
    /// Use this type if the rule does not aim at being
    /// matched directly but is always used as dependency.
    /// Rule will NEVER be used to update [crate::ScanResult]
    Dependency,
}

impl Default for Type {
    fn default() -> Self {
        Self::Detection
    }
}

/// Decision determining whether a rule should include or exclude matching events.
///
/// The `Decision` enum controls how rules affect scan results when they match:
///
/// - `Include`: Matching events are added to the scan result (default behavior)
/// - `Exclude`: Matching events are excluded from the scan result
///
/// # Serialization
///
/// This enum serializes to lowercase strings: `"include"` and `"exclude"`.
///
/// # Examples
///
/// ```yaml
/// # Explicit include (same as default)
/// name: example.include
/// decision: include
/// matches:
///     $a: .field == "value"
/// condition: $a
///
/// # Explicit exclude
/// name: example.exclude
/// decision: exclude
/// matches:
///     $a: .field == "bad_value"
/// condition: $a
/// ```
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "lowercase")]
pub enum Decision {
    /// Include matching events in the scan result.
    ///
    /// When a rule with this decision matches, the event is added to the scan
    /// result.
    Include,

    /// Exclude matching events from the scan result.
    ///
    /// When a rule with this decision matches, the event is excluded from the
    /// scan result.
    Exclude,
}

impl Default for Decision {
    /// Returns the default decision type.
    ///
    /// The default is `Decision::Include`, meaning rules without an explicit
    /// decision field will include matching events in scan results.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use gene::rules::Decision;
    ///
    /// let decision = Decision::default();
    /// assert!(matches!(decision, Decision::Include));
    /// ```
    fn default() -> Self {
        Self::Include
    }
}

impl Decision {
    /// Returns `true` if this is an include decision.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use gene::rules::Decision;
    ///
    /// let decision = Decision::Include;
    /// assert!(decision.is_include());
    ///
    /// let decision = Decision::Exclude;
    /// assert!(!decision.is_include());
    /// ```
    #[inline(always)]
    pub fn is_include(&self) -> bool {
        matches!(self, Self::Include)
    }

    /// Returns `true` if this is an exclude decision.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use gene::rules::Decision;
    ///
    /// let decision = Decision::Exclude;
    /// assert!(decision.is_exclude());
    ///
    /// let decision = Decision::Include;
    /// assert!(!decision.is_exclude());
    /// ```
    #[inline(always)]
    pub fn is_exclude(&self) -> bool {
        matches!(self, Self::Exclude)
    }
}

/// Metadata attributes of a rule
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Meta {
    /// free text tags associated to the rule
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<HashSet<String>>,
    /// [MITRE ATT&CK](https://attack.mitre.org/) ids concerned by this rule
    /// This is not a free-text field, when the rule compiles a format checking
    /// made on the ids.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attack: Option<HashSet<String>>,
    /// authors of the rule
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authors: Option<Vec<String>>,
    /// any comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comments: Option<Vec<String>>,
}

/// Miscellaneous parameters of the rule
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// whether to disable the rule or not
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disable: Option<bool>,
}

/// Defines on which kind of events the rule must match
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MatchOn {
    /// A map where **keys** are event sources
    /// and **values** are sets of event ids corresponding to that
    /// source. The rule will apply to any event matching
    /// **one** of the source and **one** of its associated event id.
    /// To match all events from a source just leave an empty set of
    /// event ids.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub events: Option<HashMap<String, HashSet<i64>>>,
}

/// Structure defining rule loadable in the [`Engine`](crate::Engine)
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Rule {
    /// Name of the rule.
    ///
    /// A unique identifier for the rule used for referencing and debugging.
    pub name: String,

    /// Type of the rule.
    ///
    /// Determines the rule's behavior in the engine. When `None`, defaults to
    /// [`Type::Detection`]. See [`Type`] enum for available variants.
    #[serde(rename = "type")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ty: Option<Type>,

    /// Decision for how this rule affects scan results.
    ///
    /// Controls whether matching events are included or excluded. When `None`,
    /// defaults to [`Decision::Include`]. See [`Decision`] enum for details.
    pub decision: Option<Decision>,

    /// Metadata associated with the rule.
    ///
    /// Contains additional information such as tags, attack IDs, authors, and
    /// comments. Used for categorization, attribution, and documentation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<Meta>,

    /// Miscellaneous parameters for the rule.
    ///
    /// Currently supports disabling rules via the `disable` parameter.
    /// When `None`, all parameters use their default values.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Params>,

    /// Directives controlling which events this rule applies to.
    ///
    /// Specifies event sources and IDs that this rule should match against.
    /// When `None`, the rule applies to all events.
    #[serde(rename = "match-on")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub match_on: Option<MatchOn>,

    /// Field matching expressions for the rule.
    ///
    /// Maps operand names to matching expressions that extract and compare
    /// field values from events. Operand names must start with `$`.
    /// When `None`, the rule matches all events.
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(deserialize_with = "deserialize_uk_hashmap")]
    pub matches: Option<HashMap<String, String>>,

    /// Condition determining when this rule triggers.
    ///
    /// Specifies how the match operands should be combined using logical
    /// operators. Common values include `all of them`, `any of them`, or
    /// custom operand combinations. When `None`, condition is always true.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<String>,

    /// Severity level for events matching this rule.
    ///
    /// Numerical value from 0 to [`MAX_SEVERITY`] indicating the importance
    /// or severity of detected events. Higher values indicate more severe events.
    /// When `None`, defaults to 0. Multiple matching rules' severities are summed
    /// and bounded by [`MAX_SEVERITY`].
    #[serde(skip_serializing_if = "Option::is_none")]
    pub severity: Option<u8>,

    /// Actions to take when this rule triggers.
    ///
    /// Set of strings representing actions that should be performed when the
    /// rule matches an event. Actions are used by external systems to determine
    /// what responses or notifications should be generated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actions: Option<HashSet<String>>,
}

impl Rule {
    /// Deserializes rules from a YAML reader.
    ///
    /// Reads and parses YAML rule definitions from the provided reader, returning
    /// a vector of results. Each result is either a successfully parsed `Rule` or
    /// a `serde_yaml::Error` indicating what went wrong during deserialization.
    ///
    /// This method is useful for loading rules from files or other I/O sources.
    #[inline]
    pub fn deserialize_reader<R: io::Read>(r: R) -> Vec<Result<Self, serde_yaml::Error>> {
        serde_yaml::Deserializer::from_reader(r)
            .map(Rule::deserialize)
            .collect()
    }

    /// Returns `true` if this rule is disabled.
    ///
    /// Checks the rule's parameters for a `disable` flag. Returns `false` if the
    /// parameter is not set or if the rule has no parameters. Disabled rules are
    /// skipped during engine processing.
    #[inline(always)]
    pub fn is_disabled(&self) -> bool {
        self.params
            .as_ref()
            .and_then(|p| p.disable)
            .unwrap_or_default()
    }

    /// Applies templates to this rule.
    ///
    /// Replaces template placeholders in the rule's match expressions with actual
    /// values from the provided templates. This allows for rule reuse and parameterization.
    /// Returns the modified rule with templates applied.
    ///
    /// # Notes
    ///
    /// This method consumes `self` and returns a new rule with templates applied.
    /// Template replacement is performed in-place on the rule's match expressions.
    #[inline]
    pub fn apply_templates(mut self, templates: &Templates) -> Self {
        templates.replace(&mut self);
        self
    }

    // build filter for events to include (positive values in
    // `match-on` section)
    fn build_include_events(
        filters: &HashMap<String, HashSet<i64>>,
    ) -> HashMap<String, HashSet<i64>> {
        let mut inc = HashMap::new();
        for (source, events) in filters {
            let events = events
                .iter()
                .filter(|&&id| id >= 0)
                .cloned()
                .collect::<HashSet<i64>>();
            if !events.is_empty() {
                inc.insert(source.clone(), events);
            }
        }
        inc
    }

    // build filter for events to exclude (negative values in
    // `match-on` section)
    fn build_exclude_events(
        filters: &HashMap<String, HashSet<i64>>,
    ) -> HashMap<String, HashSet<i64>> {
        let mut excl = HashMap::new();
        for (source, events) in filters {
            let events = events
                .iter()
                .filter(|&&id| id < 0)
                .map(|id| id.abs())
                .collect::<HashSet<i64>>();
            if !events.is_empty() {
                excl.insert(source.clone(), events);
            }
        }
        excl
    }

    #[inline]
    /// Compiles this rule into an executable form.
    ///
    /// Transforms the rule definition into a `CompiledRule` that can be executed
    /// by the engine. This process includes:
    /// - Validating the rule structure and syntax
    /// - Parsing match expressions and conditions
    /// - Setting up attack ID validation and metadata
    /// - Resolving rule dependencies
    /// - Applying default values where necessary
    ///
    /// # Errors
    ///
    /// Returns `Error` if the rule contains invalid syntax, references non-existent
    /// dependent rules, uses malformed attack IDs, or has other compilation issues.
    /// The error will include the rule name for context.
    ///
    /// # Notes
    ///
    /// This method consumes `self` and returns either the compiled rule or an error.
    /// The compilation process ensures that only valid, executable rules are loaded
    /// into the engine.
    pub fn compile_into(self) -> Result<CompiledRule, Error> {
        let name = self.name.clone();
        let filters = self.match_on.and_then(|mo| mo.events).unwrap_or_default();

        // to wrap error with rule name
        || -> Result<CompiledRule, Error> {
            let mut c = CompiledRule {
                name: self.name,
                ty: self.ty.unwrap_or_default(),
                decision: self.decision.unwrap_or_default(),
                depends: HashSet::new(),
                tags: HashSet::new(),
                attack: HashSet::new(),
                include_events: Self::build_include_events(&filters),
                exclude_events: Self::build_exclude_events(&filters),
                matches: HashMap::new(),
                condition: match self.condition {
                    Some(cond) => {
                        Condition::from_str(&cond).map_err(|e| Error::from(Box::new(e)))?
                    }
                    None => Condition::default(),
                },
                severity: bound_severity(self.severity.unwrap_or_default()),
                actions: self.actions.unwrap_or_default(),
            };

            // populating attack
            if let Some(meta) = self.meta {
                // setting tags
                c.tags = meta.tags.unwrap_or_default();

                // setting attack ids
                if let Some(attack) = meta.attack {
                    // we make sure attack id is correct
                    for r in attack.iter().map(AttackId::parse) {
                        c.attack
                            .insert(r.map_err(|e| Error::Compile(e.to_string()))?.into());
                    }
                }
            }

            // initializing operands
            if let Some(matches) = self.matches {
                for (operand, s) in matches.iter() {
                    if !operand.starts_with('$') {
                        return Err(Error::Compile(format!(
                            "operand must start with $, try with ${operand}"
                        )));
                    }
                    let m = Match::from_str(s)?;
                    // we update the list of dependent rules
                    if let Match::Rule(r) = &m {
                        c.depends.insert(r.rule_name().into());
                    }
                    c.matches.insert(operand.clone(), m);
                }
            }

            Ok(c)
        }()
        .map_err(|e| e.wrap(name))
    }
}

impl FromStr for Rule {
    type Err = serde_yaml::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        serde_yaml::from_str(s)
    }
}

#[derive(Debug, Default, Clone)]
/// Compiled form of a rule ready for execution by the engine.
///
/// This struct represents a rule that has been parsed, validated, and compiled
/// into an executable form. It contains all the information needed for the engine
/// to efficiently evaluate the rule against events, including parsed match expressions,
/// compiled conditions, and optimized data structures.
///
/// `CompiledRule` is created by the [`Rule::compile_into()`] method and is the internal
/// representation used by the engine during scanning. Unlike the source [`Rule`],
/// this compiled form is optimized for performance and contains resolved references.
///
/// # Performance Characteristics
///
/// The compiled form uses optimized data structures:
/// - `HashSet` for O(1) lookups of tags, attack IDs, and actions
/// - `HashMap` for efficient field match expression access
/// - Pre-parsed conditions for faster evaluation
/// - Event filtering maps for quick event matching checks
///
/// This optimization enables the engine to process events efficiently even with
/// large numbers of loaded rules.
pub struct CompiledRule {
    pub(crate) name: String,
    pub(crate) ty: Type,
    pub(crate) decision: Decision,
    pub(crate) depends: HashSet<String>,
    pub(crate) tags: HashSet<String>,
    pub(crate) attack: HashSet<String>,
    pub(crate) include_events: HashMap<String, HashSet<i64>>,
    pub(crate) exclude_events: HashMap<String, HashSet<i64>>,
    pub(crate) matches: HashMap<String, Match>,
    pub(crate) condition: condition::Condition,
    pub(crate) severity: u8,
    pub(crate) actions: HashSet<String>,
}

/// Error types that can occur during rule processing and compilation.
///
/// This enum represents all possible errors that can occur when working with rules,
/// including parsing, compilation, and evaluation errors. Errors can be wrapped
/// to provide context about where they occurred in the rule processing pipeline.
#[derive(Debug, Error, PartialEq)]
pub enum Error {
    /// Wrapped error with additional context.
    ///
    /// This variant is used to add contextual information about where an error
    /// occurred, typically including the rule name and the underlying error.
    /// The first string parameter is the rule name, and the second is a boxed
    /// error that occurred during processing that rule.
    #[error("rule={0} {1}")]
    Wrap(String, Box<Error>),

    /// Compilation error that occurred during rule processing.
    ///
    /// This variant represents errors that occur when compiling.
    /// The string contains a descriptive error message
    /// explaining what went wrong during compilation.
    #[error("compile error: {0}")]
    Compile(String),

    /// Error that occurred while parsing match expressions.
    ///
    /// This variant is used when there are syntax errors or invalid patterns
    /// in rule match expressions. It wraps errors from the matcher module.
    #[error("{0}")]
    ParseMatch(#[from] matcher::Error),

    /// Error that occurred while evaluating rule conditions.
    ///
    /// This variant represents errors in rule condition evaluation, such as
    /// invalid operators, type mismatches, or missing fields. It wraps errors
    /// from the condition evaluation module.
    #[error("{0}")]
    Condition(#[from] Box<condition::Error>),
}

impl Error {
    fn wrap(self, name: String) -> Self {
        Self::Wrap(name, Box::new(self))
    }

    /// Returns the innermost wrapped error.
    ///
    /// This method unwraps nested `Wrap` variants to return the underlying error,
    /// which is useful for error handling and reporting. If this error is not a
    /// `Wrap` variant, it returns itself.
    pub fn wrapped(&self) -> &Self {
        match self {
            Self::Wrap(_, e) => e,
            _ => self,
        }
    }
}

impl TryFrom<Rule> for CompiledRule {
    type Error = Error;
    fn try_from(r: Rule) -> Result<Self, Self::Error> {
        r.compile_into()
    }
}

impl CompiledRule {
    // keep this function not to break tests
    #[allow(dead_code)]
    #[inline(always)]
    fn match_event<E>(&self, event: &E) -> Result<bool, Error>
    where
        E: for<'e> Event<'e>,
    {
        self.condition
            .compute_for_event(event, &self.matches, &HashMap::new())
            .map_err(|e| Box::new(e).into())
            .map_err(|e: Error| e.wrap(self.name.clone()))
    }

    #[inline(always)]
    pub(crate) fn match_event_with_states<E>(
        &self,
        event: &E,
        rules_states: &HashMap<Cow<'_, str>, bool>,
    ) -> Result<bool, Error>
    where
        E: for<'e> Event<'e>,
    {
        self.condition
            .compute_for_event(event, &self.matches, rules_states)
            .map_err(|e| Box::new(e).into())
            .map_err(|e: Error| e.wrap(self.name.clone()))
    }

    #[inline(always)]
    pub(crate) fn can_match_on<S: AsRef<str>>(&self, src: S, id: i64) -> bool {
        // we have no filter at all
        if self.include_events.is_empty() && self.exclude_events.is_empty() {
            return true;
        }

        // explicit event excluding logic
        let opt_exclude = self.exclude_events.get(src.as_ref());
        if let Some(exclude) = opt_exclude {
            // we definitely want to exclude that event
            if exclude.contains(&id) {
                return false;
            }
        }

        let opt_include = self.include_events.get(src.as_ref());
        // we include if we have no include filter for this source
        // but we have an exclude filter (that didn't match)
        if opt_include.is_none() && opt_exclude.is_some() {
            return true;
        }

        // we return result of lookup in include filter if there is one
        if let Some(include) = opt_include {
            return include.contains(&id);
        }

        // default we cannot match on event
        false
    }

    /// Gives a read only access to the rule's name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Gives a read only access to the rule's severity
    pub fn severity(&self) -> u8 {
        self.severity
    }

    /// Returns true if the rule is [`Type::Filter`]
    #[inline(always)]
    pub fn is_filter(&self) -> bool {
        matches!(self.ty, Type::Filter)
    }

    /// Returns true if the rule is [`Type::Detection`]
    #[inline(always)]
    pub fn is_detection(&self) -> bool {
        matches!(self.ty, Type::Detection)
    }

    /// Returns `true` if this rule has an include decision.
    pub fn is_include(&self) -> bool {
        self.decision.is_include()
    }

    /// Returns `true` if this rule has an exclude decision.
    pub fn is_exclude(&self) -> bool {
        self.decision.is_exclude()
    }

    /// Returns rule's [`Type`]
    #[inline(always)]
    pub fn ty(&self) -> Type {
        self.ty
    }
}

#[cfg(test)]
mod test {
    use std::borrow::Cow;
    use std::net::IpAddr;
    use std::path::PathBuf;

    use super::*;
    use crate::{Event, FieldGetter, FieldValue, FieldNameIterator};
    use gene_derive::{Event, FieldGetter};

    macro_rules! def_event {
            // Match for a struct with fields and field attributes
            ($struct_vis:vis struct $struct_name:ident { $($(#[$field_meta:meta])* $vis:vis $field_name:ident : $field_type:ty),* $(,)? }) => {
                #[derive(Debug, Event, FieldGetter)]
                #[event(id = 42, source = "test".into())]
                $struct_vis struct $struct_name {
                    $(
                        $(#[$field_meta])*
                        $vis $field_name: $field_type
                    ),*
                }
            };
        }

    macro_rules! fake_event {
        ($name:tt, $(($path:literal, $value:expr)),*) => {
            struct $name {}

            impl<'f> FieldGetter<'f> for $name{

                fn get_from_iter(&self, _: FieldNameIterator<'_>) -> Option<$crate::FieldValue<'_>>{
                    unimplemented!()
                }

                fn get_from_path(&self, path: &crate::XPath) -> Option<$crate::FieldValue<'_>> {
                    match path.to_string_lossy().as_ref() {
                        $($path => Some($value.into()),)*
                        _ => None,
                    }
                }
            }

            impl<'e> Event<'e> for $name {

                fn id(&self) -> i64{
                    42
                }

                fn source(&self) -> Cow<'_,str> {
                    "test".into()
                }
            }
        };
    }

    #[test]
    fn test_attack_re() {
        // all the things that should match
        ["T1234", "T1234.456", "TA0043", "S1088", "G1019"]
            .iter()
            .for_each(|s| {
                assert!(ATTACK_ID_RE.is_match(&s.to_uppercase()));
                assert!(ATTACK_ID_RE.is_match(&s.to_lowercase()))
            });

        // all the things that should not match
        ["t1245sTrInG", "t1245-456", "TA_1234", "S 0001"]
            .iter()
            .for_each(|s| assert!(!ATTACK_ID_RE.is_match(s)));
    }

    #[test]
    fn test_serialize_yaml() {
        let r = Rule {
            name: "test".into(),
            ..Default::default()
        };
        let s = serde_yaml::to_string(&r).unwrap();
        let d: Rule = serde_yaml::from_str(&s).unwrap();
        assert_eq!(d.name, "test");
    }

    #[test]
    fn test_rule_match() {
        let test = r#"
---
name: test
matches:
    $a: .data.exe.file == '/bin/ls'
    $b: .data.exe.size > '42'
    $c: .data.exe.size >= '43'
    $d: .data.exe.size < '4242'
    $e: .data.exe.size <= '43'
    $f: .data.exe.perm &= '0x40'
    $g: .data.exe.file ~= '^/bin/ls$'
    $h: .data.exe.file ~= '(?i:/BIN/LS)'
    $i: .data.exe.file == @.data.exe.file
    $k: .data.exe.file == @.data.exe.size
    $l: .data.exe.size == '43'
    $m: .data.exe.size == '0x2b'
condition: $a and $b and $c and $d and $e and $f and $g and $h and $i and not $k and $l and $m
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d)
            .map_err(|e| println!("{e}"))
            .unwrap();

        fake_event!(
            LsEvent,
            (".data.exe.file", "/bin/ls"),
            (".data.exe.size", 43),
            // this should trigger a string convertion to Number
            (".data.exe.perm", "0x10040")
        );

        assert!(cr.match_event(&(LsEvent {})).unwrap());
    }

    #[test]
    fn test_incompatible_fields() {
        let test = r#"
---
name: test
matches:
    $b: .data.exe.size > '42'
condition: $b
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        // we need to put something that cannot be transformed to a Number
        fake_event!(Dummy, (".data.exe.size", "42*3"));
        assert!(cr.match_event(&(Dummy {})).is_err_and(|e| {
            eprintln!("{e}");
            matches!(e.wrapped(), Error::Condition(_))
        }));
    }

    #[test]
    fn test_unknown_fields() {
        let test = r#"
---
name: test
matches:
    $b: .data.not_existing_field > '42'
condition: $b
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        fake_event!(Dummy, (".data.exe.size", "43"));
        assert!(cr.match_event(&(Dummy {})).is_err_and(|e| {
            eprintln!("{e}");
            matches!(e.wrapped(), Error::Condition(_))
        }));
    }

    #[test]
    fn test_unknown_operand() {
        let test = r#"
---
name: test
matches:
    $b: .data.not_existing_field > '42'
condition: $c
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        fake_event!(Dummy, (".data.exe.size", "43"));
        assert!(cr.match_event(&(Dummy {})).is_err_and(|e| {
            eprintln!("{e}");
            matches!(e.wrapped(), Error::Condition(_))
        }));
    }

    #[test]
    fn test_match_all_rule_operand() {
        let test = r#"
---
name: test
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        fake_event!(Dummy, (".data.exe.size", "43"));

        assert!(cr.match_event(&(Dummy {})).unwrap());
    }

    #[test]
    fn test_path_buf_matching() {
        let test = r#"
---
name: test
match-on:
    events:
        test: [42]
matches:
    $a: .path == "/some/path"
condition: $a
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                path: PathBuf,
            }
        );

        assert!(cr
            .match_event(
                &(Dummy {
                    path: PathBuf::from("/some/path")
                })
            )
            .unwrap());
    }

    #[test]
    fn test_ip_addr_matching() {
        let test = r#"
---
name: test
match-on:
    events:
        test: [42]
matches:
    $a: .ip == "8.8.4.4"
    #starts with 8.8
    $b: .ip ~= "^8\.8\."
condition: $a and $b
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                ip: IpAddr,
            }
        );

        assert!(cr
            .match_event(
                &(Dummy {
                    ip: "8.8.4.4".parse().unwrap(),
                })
            )
            .unwrap());
    }

    #[test]
    fn test_templates() {
        let test = r#"
---
name: test
matches:
    $a: '{{path}} == "{{pattern}}"'
    $b: .ip ~= "^8\.8\."
condition: $a and $b
..."#;

        let mut templates = HashMap::new();
        templates.insert("path".to_string(), ".data.file.exe".to_string());
        templates.insert("pattern".into(), "8.8.4.4".into());

        let d = serde_yaml::from_str::<'_, Rule>(test)
            .unwrap()
            .apply_templates(&templates.into());

        let matches = d.matches.unwrap();
        let m = matches.get("$a").unwrap();
        assert_eq!(m, r#".data.file.exe == "8.8.4.4""#);
    }

    #[test]
    fn test_all_of_them() {
        let test = r#"
---
name: test
matches:
    $a: .ip == "8.8.4.4"
    $b: .ip ~= "^8\.8\."
condition: all of them
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                ip: IpAddr,
            }
        );

        let event = Dummy {
            ip: "8.8.4.4".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));
    }

    #[test]
    fn test_all_of_vars() {
        let test = r#"
---
name: test
matches:
    $ip1: .ip == "8.8.4.4"
    $ip2: .ip ~= "^8\.8\."
    $t : .ip == "4.4.4.4"
condition: all of $ip
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                ip: IpAddr,
            }
        );

        let event = Dummy {
            ip: "8.8.4.4".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));
    }

    #[test]
    fn test_any_of_them() {
        let test = r#"
---
name: test
matches:
    $a: .ip == "8.8.4.4"
    $b: .ip ~= "^8\.8\."
condition: any of them
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                ip: IpAddr,
            }
        );

        let event = Dummy {
            ip: "8.8.42.42".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));
    }

    #[test]
    fn test_any_of_vars() {
        let test = r#"
---
name: test
matches:
    $ip2: .ip == "42.42.42.42"
    $ip3: .ip == "8.8.4.4"
condition: any of $ip
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                ip: IpAddr,
            }
        );

        for (ip, expect) in [
            ("42.42.42.42", true),
            ("8.8.4.4", true),
            ("255.0.0.0", false),
        ] {
            let event = Dummy {
                ip: ip.parse().unwrap(),
            };

            assert_eq!(cr.match_event(&event), Ok(expect));
        }
    }

    #[test]
    fn test_n_of_them() {
        let test = r#"
---
name: test
matches:
    $path1: .path == "/bin/ls"
    $ip2: .ip == "42.42.42.42"
    $ip3: .ip == "8.8.4.4"
condition: 2 of them
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                path: String,
                ip: IpAddr,
            }
        );

        let event = Dummy {
            path: "/bin/ls".into(),
            ip: "42.42.42.42".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));
    }

    #[test]
    fn test_n_of_vars() {
        let test = r#"
---
name: test
matches:
    $path1: .path == "/bin/ls"
    $path2: .path == "/bin/true"
    $ip1: .ip == "42.42.42.42"
    $ip2: .ip == "8.8.4.4"
condition: 1 of $path or 1 of $ip
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                path: String,
                ip: IpAddr,
            }
        );

        let event = Dummy {
            path: "/bin/ls".into(),
            ip: "42.42.42.42".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));

        let event = Dummy {
            path: "/bin/true".into(),
            ip: "8.8.4.4".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));
    }

    #[test]
    fn test_none_of_them() {
        let test = r#"
---
name: test
matches:
    $a: .ip == "8.8.4.4"
    $b: .ip ~= "^8\.8\."
condition: none of them
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                ip: IpAddr,
            }
        );

        let event = Dummy {
            ip: "42.42.42.42".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));
    }

    #[test]
    fn test_none_of_vars() {
        let test = r#"
---
name: test
matches:
    $ip1: .ip == "8.8.4.4"
    $ip2: .ip ~= "^8\.8\."
condition: none of $ip
..."#;

        let d: Rule = serde_yaml::from_str(test).unwrap();
        let cr = CompiledRule::try_from(d).unwrap();

        def_event!(
            pub struct Dummy {
                ip: IpAddr,
            }
        );

        let event = Dummy {
            ip: "42.42.42.42".parse().unwrap(),
        };

        assert_eq!(cr.match_event(&event), Ok(true));
    }

    #[test]
    fn test_deserialization_error() {
        let test = r#"
---
name: test
matches:
    $ip: .ip == "8.8.4.4"
    $ip: .ip ~= "^8\.8\."
condition: none of $ip
..."#;

        assert!(serde_yaml::from_str::<Rule>(test).is_err());
    }
}