dd_sds/
rule_match.rs

1use crate::match_validation::match_status::MatchStatus;
2use crate::{encoding::Encoding, path::Path, StringMatch};
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 new(rule_index: usize, string_match: StringMatch) -> Self {
59        Self {
60            rule_index,
61            utf8_start: string_match.start,
62            utf8_end: string_match.end,
63            custom_start: E::zero_index(),
64            custom_end: E::zero_index(),
65        }
66    }
67
68    pub fn len(&self) -> usize {
69        self.utf8_end - self.utf8_start
70    }
71}
72
73#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
74pub enum ReplacementType {
75    None,
76    Placeholder,
77    Hash,
78    PartialStart,
79    PartialEnd,
80}
81
82impl Display for ReplacementType {
83    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
84        match self {
85            ReplacementType::None => write!(f, "none"),
86            ReplacementType::Placeholder => write!(f, "placeholder"),
87            ReplacementType::Hash => write!(f, "hash"),
88            ReplacementType::PartialStart => write!(f, "partial_beginning"),
89            ReplacementType::PartialEnd => write!(f, "partial_end"),
90        }
91    }
92}