/build/cargo-vendor-dir/aho-corasick-1.1.2/src/util/error.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::util::{ |
2 | | primitives::{PatternID, SmallIndex}, |
3 | | search::MatchKind, |
4 | | }; |
5 | | |
6 | | /// An error that occurred during the construction of an Aho-Corasick |
7 | | /// automaton. |
8 | | /// |
9 | | /// Build errors occur when some kind of limit has been exceeded, either in the |
10 | | /// number of states, the number of patterns of the length of a pattern. These |
11 | | /// limits aren't part of the public API, but they should generally be large |
12 | | /// enough to handle most use cases. |
13 | | /// |
14 | | /// When the `std` feature is enabled, this implements the `std::error::Error` |
15 | | /// trait. |
16 | | #[derive(Clone, Debug)] |
17 | | pub struct BuildError { |
18 | | kind: ErrorKind, |
19 | | } |
20 | | |
21 | | /// The kind of error that occurred. |
22 | | #[derive(Clone, Debug)] |
23 | | enum ErrorKind { |
24 | | /// An error that occurs when allocating a new state would result in an |
25 | | /// identifier that exceeds the capacity of a `StateID`. |
26 | | StateIDOverflow { |
27 | | /// The maximum possible id. |
28 | | max: u64, |
29 | | /// The maximum ID requested. |
30 | | requested_max: u64, |
31 | | }, |
32 | | /// An error that occurs when adding a pattern to an Aho-Corasick |
33 | | /// automaton would result in an identifier that exceeds the capacity of a |
34 | | /// `PatternID`. |
35 | | PatternIDOverflow { |
36 | | /// The maximum possible id. |
37 | | max: u64, |
38 | | /// The maximum ID requested. |
39 | | requested_max: u64, |
40 | | }, |
41 | | /// Occurs when a pattern string is given to the Aho-Corasick constructor |
42 | | /// that is too long. |
43 | | PatternTooLong { |
44 | | /// The ID of the pattern that was too long. |
45 | | pattern: PatternID, |
46 | | /// The length that was too long. |
47 | | len: usize, |
48 | | }, |
49 | | } |
50 | | |
51 | | impl BuildError { |
52 | 0 | pub(crate) fn state_id_overflow( |
53 | 0 | max: u64, |
54 | 0 | requested_max: u64, |
55 | 0 | ) -> BuildError { |
56 | 0 | BuildError { kind: ErrorKind::StateIDOverflow { max, requested_max } } |
57 | 0 | } |
58 | | |
59 | 0 | pub(crate) fn pattern_id_overflow( |
60 | 0 | max: u64, |
61 | 0 | requested_max: u64, |
62 | 0 | ) -> BuildError { |
63 | 0 | BuildError { |
64 | 0 | kind: ErrorKind::PatternIDOverflow { max, requested_max }, |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | 0 | pub(crate) fn pattern_too_long( |
69 | 0 | pattern: PatternID, |
70 | 0 | len: usize, |
71 | 0 | ) -> BuildError { |
72 | 0 | BuildError { kind: ErrorKind::PatternTooLong { pattern, len } } |
73 | 0 | } |
74 | | } |
75 | | |
76 | | #[cfg(feature = "std")] |
77 | | impl std::error::Error for BuildError {} |
78 | | |
79 | | impl core::fmt::Display for BuildError { |
80 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
81 | 0 | match self.kind { |
82 | 0 | ErrorKind::StateIDOverflow { max, requested_max } => { |
83 | 0 | write!( |
84 | 0 | f, |
85 | 0 | "state identifier overflow: failed to create state ID \ |
86 | 0 | from {}, which exceeds the max of {}", |
87 | 0 | requested_max, max, |
88 | 0 | ) |
89 | | } |
90 | 0 | ErrorKind::PatternIDOverflow { max, requested_max } => { |
91 | 0 | write!( |
92 | 0 | f, |
93 | 0 | "pattern identifier overflow: failed to create pattern ID \ |
94 | 0 | from {}, which exceeds the max of {}", |
95 | 0 | requested_max, max, |
96 | 0 | ) |
97 | | } |
98 | 0 | ErrorKind::PatternTooLong { pattern, len } => { |
99 | 0 | write!( |
100 | 0 | f, |
101 | 0 | "pattern {} with length {} exceeds \ |
102 | 0 | the maximum pattern length of {}", |
103 | 0 | pattern.as_usize(), |
104 | 0 | len, |
105 | 0 | SmallIndex::MAX.as_usize(), |
106 | 0 | ) |
107 | | } |
108 | | } |
109 | 0 | } |
110 | | } |
111 | | |
112 | | /// An error that occurred during an Aho-Corasick search. |
113 | | /// |
114 | | /// An error that occurs during a search is limited to some kind of |
115 | | /// misconfiguration that resulted in an illegal call. Stated differently, |
116 | | /// whether an error occurs is not dependent on the specific bytes in the |
117 | | /// haystack. |
118 | | /// |
119 | | /// Examples of misconfiguration: |
120 | | /// |
121 | | /// * Executing a stream or overlapping search on a searcher that was built was |
122 | | /// something other than [`MatchKind::Standard`](crate::MatchKind::Standard) |
123 | | /// semantics. |
124 | | /// * Requested an anchored or an unanchored search on a searcher that doesn't |
125 | | /// support unanchored or anchored searches, respectively. |
126 | | /// |
127 | | /// When the `std` feature is enabled, this implements the `std::error::Error` |
128 | | /// trait. |
129 | | #[derive(Clone, Debug, Eq, PartialEq)] |
130 | | pub struct MatchError(alloc::boxed::Box<MatchErrorKind>); |
131 | | |
132 | | impl MatchError { |
133 | | /// Create a new error value with the given kind. |
134 | | /// |
135 | | /// This is a more verbose version of the kind-specific constructors, e.g., |
136 | | /// `MatchError::unsupported_stream`. |
137 | 0 | pub fn new(kind: MatchErrorKind) -> MatchError { |
138 | 0 | MatchError(alloc::boxed::Box::new(kind)) |
139 | 0 | } |
140 | | |
141 | | /// Returns a reference to the underlying error kind. |
142 | 0 | pub fn kind(&self) -> &MatchErrorKind { |
143 | 0 | &self.0 |
144 | 0 | } |
145 | | |
146 | | /// Create a new "invalid anchored search" error. This occurs when the |
147 | | /// caller requests an anchored search but where anchored searches aren't |
148 | | /// supported. |
149 | | /// |
150 | | /// This is the same as calling `MatchError::new` with a |
151 | | /// [`MatchErrorKind::InvalidInputAnchored`] kind. |
152 | 0 | pub fn invalid_input_anchored() -> MatchError { |
153 | 0 | MatchError::new(MatchErrorKind::InvalidInputAnchored) |
154 | 0 | } |
155 | | |
156 | | /// Create a new "invalid unanchored search" error. This occurs when the |
157 | | /// caller requests an unanchored search but where unanchored searches |
158 | | /// aren't supported. |
159 | | /// |
160 | | /// This is the same as calling `MatchError::new` with a |
161 | | /// [`MatchErrorKind::InvalidInputUnanchored`] kind. |
162 | 0 | pub fn invalid_input_unanchored() -> MatchError { |
163 | 0 | MatchError::new(MatchErrorKind::InvalidInputUnanchored) |
164 | 0 | } |
165 | | |
166 | | /// Create a new "unsupported stream search" error. This occurs when the |
167 | | /// caller requests a stream search while using an Aho-Corasick automaton |
168 | | /// with a match kind other than [`MatchKind::Standard`]. |
169 | | /// |
170 | | /// The match kind given should be the match kind of the automaton. It |
171 | | /// should never be `MatchKind::Standard`. |
172 | 0 | pub fn unsupported_stream(got: MatchKind) -> MatchError { |
173 | 0 | MatchError::new(MatchErrorKind::UnsupportedStream { got }) |
174 | 0 | } |
175 | | |
176 | | /// Create a new "unsupported overlapping search" error. This occurs when |
177 | | /// the caller requests an overlapping search while using an Aho-Corasick |
178 | | /// automaton with a match kind other than [`MatchKind::Standard`]. |
179 | | /// |
180 | | /// The match kind given should be the match kind of the automaton. It |
181 | | /// should never be `MatchKind::Standard`. |
182 | 0 | pub fn unsupported_overlapping(got: MatchKind) -> MatchError { |
183 | 0 | MatchError::new(MatchErrorKind::UnsupportedOverlapping { got }) |
184 | 0 | } |
185 | | |
186 | | /// Create a new "unsupported empty pattern" error. This occurs when the |
187 | | /// caller requests a search for which matching an automaton that contains |
188 | | /// an empty pattern string is not supported. |
189 | 0 | pub fn unsupported_empty() -> MatchError { |
190 | 0 | MatchError::new(MatchErrorKind::UnsupportedEmpty) |
191 | 0 | } |
192 | | } |
193 | | |
194 | | /// The underlying kind of a [`MatchError`]. |
195 | | /// |
196 | | /// This is a **non-exhaustive** enum. That means new variants may be added in |
197 | | /// a semver-compatible release. |
198 | | #[non_exhaustive] |
199 | | #[derive(Clone, Debug, Eq, PartialEq)] |
200 | | pub enum MatchErrorKind { |
201 | | /// An error indicating that an anchored search was requested, but from a |
202 | | /// searcher that was built without anchored support. |
203 | | InvalidInputAnchored, |
204 | | /// An error indicating that an unanchored search was requested, but from a |
205 | | /// searcher that was built without unanchored support. |
206 | | InvalidInputUnanchored, |
207 | | /// An error indicating that a stream search was attempted on an |
208 | | /// Aho-Corasick automaton with an unsupported `MatchKind`. |
209 | | UnsupportedStream { |
210 | | /// The match semantics for the automaton that was used. |
211 | | got: MatchKind, |
212 | | }, |
213 | | /// An error indicating that an overlapping search was attempted on an |
214 | | /// Aho-Corasick automaton with an unsupported `MatchKind`. |
215 | | UnsupportedOverlapping { |
216 | | /// The match semantics for the automaton that was used. |
217 | | got: MatchKind, |
218 | | }, |
219 | | /// An error indicating that the operation requested doesn't support |
220 | | /// automatons that contain an empty pattern string. |
221 | | UnsupportedEmpty, |
222 | | } |
223 | | |
224 | | #[cfg(feature = "std")] |
225 | | impl std::error::Error for MatchError {} |
226 | | |
227 | | impl core::fmt::Display for MatchError { |
228 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
229 | 0 | match *self.kind() { |
230 | | MatchErrorKind::InvalidInputAnchored => { |
231 | 0 | write!(f, "anchored searches are not supported or enabled") |
232 | | } |
233 | | MatchErrorKind::InvalidInputUnanchored => { |
234 | 0 | write!(f, "unanchored searches are not supported or enabled") |
235 | | } |
236 | 0 | MatchErrorKind::UnsupportedStream { got } => { |
237 | 0 | write!( |
238 | 0 | f, |
239 | 0 | "match kind {:?} does not support stream searching", |
240 | 0 | got, |
241 | 0 | ) |
242 | | } |
243 | 0 | MatchErrorKind::UnsupportedOverlapping { got } => { |
244 | 0 | write!( |
245 | 0 | f, |
246 | 0 | "match kind {:?} does not support overlapping searches", |
247 | 0 | got, |
248 | 0 | ) |
249 | | } |
250 | | MatchErrorKind::UnsupportedEmpty => { |
251 | 0 | write!( |
252 | 0 | f, |
253 | 0 | "matching with an empty pattern string is not \ |
254 | 0 | supported for this operation", |
255 | 0 | ) |
256 | | } |
257 | | } |
258 | 0 | } |
259 | | } |