dd_sds/rule_match.rs
1use crate::match_validation::match_status::MatchStatus;
2use crate::{encoding::Encoding, path::Path};
3use std::fmt::Debug;
4use std::fmt::{Display, Formatter};
5
6/// Metadata about a rule match.
7#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
8pub struct RuleMatch {
9 /// The index of the rule that matched. This preserves the order
10 /// of rules that were passed into the scanner.
11 pub rule_index: usize,
12
13 /// The path where the match occurred
14 pub path: Path<'static>,
15
16 /// The type of replacement that happened
17 pub replacement_type: ReplacementType,
18
19 /// The start of the match. This points to the replaced text, and not the original text.
20 /// The index is based off of the encoding for the event.
21 pub start_index: usize,
22
23 /// The end, exclusive of the match. This points to the replaced text, and not
24 /// the original text.
25 /// The index is based off of the encoding for the event.
26 pub end_index_exclusive: usize,
27
28 /// the difference between the end (UTF8 byte index) of the match data in the
29 /// **INPUT** string and the end (UTF8 byte index) of the match data applied to the new **OUTPUT** string after match actions
30 /// performed.
31 pub shift_offset: isize,
32
33 // matched string copied from content. If scanner has the return_matches set to true
34 pub match_value: Option<String>,
35
36 // match status updated by the validate_matches scanner method
37 pub match_status: MatchStatus,
38}
39
40pub struct InternalRuleMatch<E: Encoding> {
41 /// index of the rule that matched
42 pub rule_index: usize,
43
44 /// The index of the start of the match from the **INPUT** string (byte index of a UTF8 string)
45 pub utf8_start: usize,
46
47 /// The index of the end of a match from the **INPUT** string, exclusive (byte index of a UTF8 string)
48 pub utf8_end: usize,
49
50 /// The start index of the match, converted to a different encoding
51 pub custom_start: <E as Encoding>::Index,
52
53 /// The end index of the match, converted to a different encoding
54 pub custom_end: <E as Encoding>::Index,
55}
56
57impl<E: Encoding> InternalRuleMatch<E> {
58 pub fn len(&self) -> usize {
59 self.utf8_end - self.utf8_start
60 }
61}
62
63#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
64pub enum ReplacementType {
65 None,
66 Placeholder,
67 Hash,
68 PartialStart,
69 PartialEnd,
70}
71
72impl Display for ReplacementType {
73 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74 match self {
75 ReplacementType::None => write!(f, "none"),
76 ReplacementType::Placeholder => write!(f, "placeholder"),
77 ReplacementType::Hash => write!(f, "hash"),
78 ReplacementType::PartialStart => write!(f, "partial_beginning"),
79 ReplacementType::PartialEnd => write!(f, "partial_end"),
80 }
81 }
82}