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
use crate::match_validation::match_status::MatchStatus;
use crate::{StringMatch, encoding::Encoding, path::Path};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::fmt::{Display, Formatter};
/// Metadata about a rule match.
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Serialize, Deserialize)]
pub struct RuleMatch {
/// The index of the rule that matched. This preserves the order
/// of rules that were passed into the scanner.
pub rule_index: usize,
/// The path where the match occurred
pub path: Path<'static>,
/// The type of replacement that happened
pub replacement_type: ReplacementType,
/// The start of the match. This points to the replaced text, and not the original text.
/// The index is based off of the encoding for the event.
pub start_index: usize,
/// The end, exclusive of the match. This points to the replaced text, and not
/// the original text.
/// The index is based off of the encoding for the event.
pub end_index_exclusive: usize,
/// the difference between the end (UTF8 byte index) of the match data in the
/// **INPUT** string and the end (UTF8 byte index) of the match data applied to the new **OUTPUT** string after match actions
/// performed.
pub shift_offset: isize,
// matched string copied from content. If scanner has the return_matches set to true
pub match_value: Option<String>,
// match status updated by the validate_matches scanner method
pub match_status: MatchStatus,
/// The keyword that was found for this match. Only some rules might set this value.
pub keyword: Option<String>,
}
pub struct InternalRuleMatch<E: Encoding> {
/// index of the rule that matched
pub rule_index: usize,
/// The index of the start of the match from the **INPUT** string (byte index of a UTF8 string)
pub utf8_start: usize,
/// The index of the end of a match from the **INPUT** string, exclusive (byte index of a UTF8 string)
pub utf8_end: usize,
/// The start index of the match, converted to a different encoding
pub custom_start: <E as Encoding>::Index,
/// The end index of the match, converted to a different encoding
pub custom_end: <E as Encoding>::Index,
/// The keyword that was found for this match. Only some rules might set this value.
pub keyword: Option<String>,
}
impl<E: Encoding> InternalRuleMatch<E> {
pub fn new(rule_index: usize, string_match: StringMatch) -> Self {
Self {
rule_index,
utf8_start: string_match.start,
utf8_end: string_match.end,
custom_start: E::zero_index(),
custom_end: E::zero_index(),
keyword: string_match.keyword,
}
}
pub fn len(&self) -> usize {
self.utf8_end - self.utf8_start
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ReplacementType {
None,
Placeholder,
Hash,
PartialStart,
PartialEnd,
}
impl Display for ReplacementType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ReplacementType::None => write!(f, "none"),
ReplacementType::Placeholder => write!(f, "placeholder"),
ReplacementType::Hash => write!(f, "hash"),
ReplacementType::PartialStart => write!(f, "partial_beginning"),
ReplacementType::PartialEnd => write!(f, "partial_end"),
}
}
}