1use crate::match_validation::match_status::MatchStatus;
2use crate::{StringMatch, encoding::Encoding, path::Path};
3use serde::{Deserialize, Serialize};
4use std::fmt::Debug;
5use std::fmt::{Display, Formatter};
6
7#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Serialize, Deserialize)]
9pub struct RuleMatch {
10 pub rule_index: usize,
13
14 pub path: Path<'static>,
16
17 pub replacement_type: ReplacementType,
19
20 pub start_index: usize,
23
24 pub end_index_exclusive: usize,
28
29 pub shift_offset: isize,
33
34 pub match_value: Option<String>,
36
37 pub match_status: MatchStatus,
39
40 pub keyword: Option<String>,
42}
43
44pub struct InternalRuleMatch<E: Encoding> {
45 pub rule_index: usize,
47
48 pub utf8_start: usize,
50
51 pub utf8_end: usize,
53
54 pub custom_start: <E as Encoding>::Index,
56
57 pub custom_end: <E as Encoding>::Index,
59
60 pub keyword: Option<String>,
62}
63
64impl<E: Encoding> InternalRuleMatch<E> {
65 pub fn new(rule_index: usize, string_match: StringMatch) -> Self {
66 Self {
67 rule_index,
68 utf8_start: string_match.start,
69 utf8_end: string_match.end,
70 custom_start: E::zero_index(),
71 custom_end: E::zero_index(),
72 keyword: string_match.keyword,
73 }
74 }
75
76 pub fn len(&self) -> usize {
77 self.utf8_end - self.utf8_start
78 }
79}
80
81#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
82pub enum ReplacementType {
83 None,
84 Placeholder,
85 Hash,
86 PartialStart,
87 PartialEnd,
88}
89
90impl Display for ReplacementType {
91 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
92 match self {
93 ReplacementType::None => write!(f, "none"),
94 ReplacementType::Placeholder => write!(f, "placeholder"),
95 ReplacementType::Hash => write!(f, "hash"),
96 ReplacementType::PartialStart => write!(f, "partial_beginning"),
97 ReplacementType::PartialEnd => write!(f, "partial_end"),
98 }
99 }
100}