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 ({}) is smaller than \
84 minimum required ({})",
85 given, minimum,
86 )
87 }
88 BuildErrorKind::InsufficientStateIDCapacity { ref err } => {
89 err.fmt(f)
90 }
91 BuildErrorKind::Unsupported(ref msg) => {
92 write!(f, "unsupported regex feature for DFAs: {}", msg)
93 }
94 }
95 }
96}
97
98#[non_exhaustive]
117#[derive(Clone, Debug)]
118pub enum StartError {
119 Cache {
122 err: CacheError,
124 },
125 Quit {
128 byte: u8,
130 },
131 UnsupportedAnchored {
134 mode: Anchored,
136 },
137}
138
139impl StartError {
140 pub(crate) fn cache(err: CacheError) -> StartError {
141 StartError::Cache { err }
142 }
143
144 pub(crate) fn quit(byte: u8) -> StartError {
145 StartError::Quit { byte }
146 }
147
148 pub(crate) fn unsupported_anchored(mode: Anchored) -> StartError {
149 StartError::UnsupportedAnchored { mode }
150 }
151}
152
153#[cfg(feature = "std")]
154impl std::error::Error for StartError {
155 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
156 match *self {
157 StartError::Cache { ref err } => Some(err),
158 _ => None,
159 }
160 }
161}
162
163impl core::fmt::Display for StartError {
164 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
165 match *self {
166 StartError::Cache { .. } => write!(
167 f,
168 "error computing start state because of cache inefficiency"
169 ),
170 StartError::Quit { byte } => write!(
171 f,
172 "error computing start state because the look-behind byte \
173 {:?} triggered a quit state",
174 crate::util::escape::DebugByte(byte),
175 ),
176 StartError::UnsupportedAnchored { mode: Anchored::Yes } => {
177 write!(
178 f,
179 "error computing start state because \
180 anchored searches are not supported or enabled"
181 )
182 }
183 StartError::UnsupportedAnchored { mode: Anchored::No } => {
184 write!(
185 f,
186 "error computing start state because \
187 unanchored searches are not supported or enabled"
188 )
189 }
190 StartError::UnsupportedAnchored {
191 mode: Anchored::Pattern(pid),
192 } => {
193 write!(
194 f,
195 "error computing start state because \
196 anchored searches for a specific pattern ({}) \
197 are not supported or enabled",
198 pid.as_usize(),
199 )
200 }
201 }
202 }
203}
204
205#[derive(Clone, Debug)]
223pub struct CacheError(());
224
225impl CacheError {
226 pub(crate) fn too_many_cache_clears() -> CacheError {
227 CacheError(())
228 }
229
230 pub(crate) fn bad_efficiency() -> CacheError {
231 CacheError(())
232 }
233}
234
235#[cfg(feature = "std")]
236impl std::error::Error for CacheError {}
237
238impl core::fmt::Display for CacheError {
239 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
240 write!(f, "lazy DFA cache has been cleared too many times")
241 }
242}