Skip to main content

fuzzy_regex/compat/
pattern.rs

1//! Pattern struct compatible with fuzzy-aho-corasick.
2
3use crate::types::{FuzzyLimits, NumEdits};
4
5/// A search pattern with optional weight and fuzzy limits.
6///
7/// Patterns can be created from strings or tuples for convenience.
8#[derive(Debug, Clone, PartialEq)]
9pub struct Pattern {
10    /// The pattern string.
11    pub pattern: String,
12    /// Length in grapheme clusters.
13    pub grapheme_len: usize,
14    /// Optional custom ID for uniqueness tracking.
15    pub custom_unique_id: Option<usize>,
16    /// Pattern weight (default 1.0).
17    pub weight: f32,
18    /// Per-pattern fuzzy limits.
19    pub limits: Option<FuzzyLimits>,
20}
21
22impl Pattern {
23    /// Get the pattern as a string slice.
24    #[must_use]
25    pub fn as_str(&self) -> &str {
26        &self.pattern
27    }
28
29    /// Get the byte length of the pattern.
30    #[must_use]
31    pub fn len(&self) -> usize {
32        self.pattern.len()
33    }
34
35    /// Check if the pattern is empty.
36    #[must_use]
37    pub fn is_empty(&self) -> bool {
38        self.pattern.is_empty()
39    }
40
41    /// Set pattern weight.
42    #[must_use]
43    pub fn weight(mut self, weight: f32) -> Self {
44        self.weight = weight;
45        self
46    }
47
48    /// Set per-pattern fuzzy limits.
49    #[must_use]
50    pub fn fuzzy(mut self, limits: FuzzyLimits) -> Self {
51        self.limits = Some(limits);
52        self
53    }
54
55    /// Set custom unique ID for pattern deduplication.
56    #[must_use]
57    pub fn custom_unique_id(mut self, id: usize) -> Self {
58        self.custom_unique_id = Some(id);
59        self
60    }
61}
62
63fn count_graphemes(s: &str) -> usize {
64    unicode_segmentation::UnicodeSegmentation::graphemes(s, true).count()
65}
66
67impl From<&str> for Pattern {
68    fn from(s: &str) -> Self {
69        Pattern {
70            pattern: s.to_owned(),
71            grapheme_len: count_graphemes(s),
72            weight: 1.0,
73            limits: None,
74            custom_unique_id: None,
75        }
76    }
77}
78
79impl From<String> for Pattern {
80    fn from(s: String) -> Self {
81        let grapheme_len = count_graphemes(&s);
82        Pattern {
83            pattern: s,
84            grapheme_len,
85            weight: 1.0,
86            limits: None,
87            custom_unique_id: None,
88        }
89    }
90}
91
92impl From<&String> for Pattern {
93    fn from(s: &String) -> Self {
94        Pattern {
95            pattern: s.clone(),
96            grapheme_len: count_graphemes(s),
97            weight: 1.0,
98            limits: None,
99            custom_unique_id: None,
100        }
101    }
102}
103
104impl From<(&str, f32)> for Pattern {
105    fn from((s, w): (&str, f32)) -> Self {
106        Pattern {
107            pattern: s.to_owned(),
108            grapheme_len: count_graphemes(s),
109            weight: w,
110            limits: None,
111            custom_unique_id: None,
112        }
113    }
114}
115
116impl From<(String, f32)> for Pattern {
117    fn from((s, w): (String, f32)) -> Self {
118        let grapheme_len = count_graphemes(&s);
119        Pattern {
120            pattern: s,
121            grapheme_len,
122            weight: w,
123            limits: None,
124            custom_unique_id: None,
125        }
126    }
127}
128
129impl From<(&String, f32)> for Pattern {
130    fn from((s, w): (&String, f32)) -> Self {
131        Pattern {
132            pattern: s.clone(),
133            grapheme_len: count_graphemes(s),
134            weight: w,
135            limits: None,
136            custom_unique_id: None,
137        }
138    }
139}
140
141impl From<(&str, f32, NumEdits)> for Pattern {
142    fn from((s, w, max_edits): (&str, f32, NumEdits)) -> Self {
143        Pattern {
144            pattern: s.to_owned(),
145            grapheme_len: count_graphemes(s),
146            weight: w,
147            limits: Some(FuzzyLimits::new().edits(max_edits)),
148            custom_unique_id: None,
149        }
150    }
151}
152
153impl From<(String, f32, NumEdits)> for Pattern {
154    fn from((s, w, max_edits): (String, f32, NumEdits)) -> Self {
155        let grapheme_len = count_graphemes(&s);
156        Pattern {
157            pattern: s,
158            grapheme_len,
159            weight: w,
160            limits: Some(FuzzyLimits::new().edits(max_edits)),
161            custom_unique_id: None,
162        }
163    }
164}