regex_automata/hybrid/
error.rs1use crate::{hybrid::id::LazyStateIDError, nfa, util::search::Anchored};
2
3#[derive(Clone, Debug)]
23pub struct BuildError {
24 kind: BuildErrorKind,
25}
26
27#[derive(Clone, Debug)]
28enum BuildErrorKind {
29 NFA(nfa::thompson::BuildError),
30 InsufficientCacheCapacity { minimum: usize, given: usize },
31 InsufficientStateIDCapacity { err: LazyStateIDError },
32 Unsupported(&'static str),
33}
34
35impl BuildError {
36 pub(crate) fn nfa(err: nfa::thompson::BuildError) -> BuildError {
37 BuildError { kind: BuildErrorKind::NFA(err) }
38 }
39
40 pub(crate) fn insufficient_cache_capacity(
41 minimum: usize,
42 given: usize,
43 ) -> BuildError {
44 BuildError {
45 kind: BuildErrorKind::InsufficientCacheCapacity { minimum, given },
46 }
47 }
48
49 pub(crate) fn insufficient_state_id_capacity(
50 err: LazyStateIDError,
51 ) -> BuildError {
52 BuildError {
53 kind: BuildErrorKind::InsufficientStateIDCapacity { err },
54 }
55 }
56
57 pub(crate) fn unsupported_dfa_word_boundary_unicode() -> BuildError {
58 let msg = "cannot build lazy DFAs for regexes with Unicode word \
59 boundaries; switch to ASCII word boundaries, or \
60 heuristically enable Unicode word boundaries or use a \
61 different regex engine";
62 BuildError { kind: BuildErrorKind::Unsupported(msg) }
63 }
64}
65
66#[cfg(feature = "std")]
67impl std::error::Error for BuildError {
68 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69 match self.kind {
70 BuildErrorKind::NFA(ref err) => Some(err),
71 _ => None,
72 }
73 }
74}
75
76impl core::fmt::Display for BuildError {
77 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
78 match self.kind {
79 BuildErrorKind::NFA(_) => write!(f, "error building NFA"),
80 BuildErrorKind::InsufficientCacheCapacity { minimum, given } => {
81 write!(
82 f,
83 "given cache capacity ({given}) is smaller than \
84 minimum required ({minimum})",
85 )
86 }
87 BuildErrorKind::InsufficientStateIDCapacity { ref err } => {
88 err.fmt(f)
89 }
90 BuildErrorKind::Unsupported(ref msg) => {
91 write!(f, "unsupported regex feature for DFAs: {msg}")
92 }
93 }
94 }
95}
96
97#[non_exhaustive]
116#[derive(Clone, Debug)]
117pub enum StartError {
118 Cache {
121 err: CacheError,
123 },
124 Quit {
127 byte: u8,
129 },
130 UnsupportedAnchored {
133 mode: Anchored,
135 },
136}
137
138impl StartError {
139 pub(crate) fn cache(err: CacheError) -> StartError {
140 StartError::Cache { err }
141 }
142
143 pub(crate) fn quit(byte: u8) -> StartError {
144 StartError::Quit { byte }
145 }
146
147 pub(crate) fn unsupported_anchored(mode: Anchored) -> StartError {
148 StartError::UnsupportedAnchored { mode }
149 }
150}
151
152#[cfg(feature = "std")]
153impl std::error::Error for StartError {
154 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
155 match *self {
156 StartError::Cache { ref err } => Some(err),
157 _ => None,
158 }
159 }
160}
161
162impl core::fmt::Display for StartError {
163 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
164 match *self {
165 StartError::Cache { .. } => write!(
166 f,
167 "error computing start state because of cache inefficiency"
168 ),
169 StartError::Quit { byte } => write!(
170 f,
171 "error computing start state because the look-behind byte \
172 {:?} triggered a quit state",
173 crate::util::escape::DebugByte(byte),
174 ),
175 StartError::UnsupportedAnchored { mode: Anchored::Yes } => {
176 write!(
177 f,
178 "error computing start state because \
179 anchored searches are not supported or enabled"
180 )
181 }
182 StartError::UnsupportedAnchored { mode: Anchored::No } => {
183 write!(
184 f,
185 "error computing start state because \
186 unanchored searches are not supported or enabled"
187 )
188 }
189 StartError::UnsupportedAnchored {
190 mode: Anchored::Pattern(pid),
191 } => {
192 write!(
193 f,
194 "error computing start state because \
195 anchored searches for a specific pattern ({}) \
196 are not supported or enabled",
197 pid.as_usize(),
198 )
199 }
200 }
201 }
202}
203
204#[derive(Clone, Debug)]
222pub struct CacheError(());
223
224impl CacheError {
225 pub(crate) fn too_many_cache_clears() -> CacheError {
226 CacheError(())
227 }
228
229 pub(crate) fn bad_efficiency() -> CacheError {
230 CacheError(())
231 }
232}
233
234#[cfg(feature = "std")]
235impl std::error::Error for CacheError {}
236
237impl core::fmt::Display for CacheError {
238 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
239 write!(f, "lazy DFA cache has been cleared too many times")
240 }
241}