| 1 |
use crate::{hybrid::id::LazyStateIDError, nfa}; |
| 2 |
|
| 3 |
/// An error that occurs when initial construction of a lazy DFA fails. |
| 4 |
/// |
| 5 |
/// A build error can occur when insufficient cache capacity is configured or |
| 6 |
/// if something about the NFA is unsupported. (For example, if one attempts |
| 7 |
/// to build a lazy DFA without heuristic Unicode support but with an NFA that |
| 8 |
/// contains a Unicode word boundary.) |
| 9 |
/// |
| 10 |
/// This error does not provide many introspection capabilities. There are |
| 11 |
/// generally only two things you can do with it: |
| 12 |
/// |
| 13 |
/// * Obtain a human readable message via its `std::fmt::Display` impl. |
| 14 |
/// * Access an underlying |
| 15 |
/// [`nfa::thompson::BuildError`](crate::nfa::thompson::BuildError) |
| 16 |
/// type from its `source` method via the `std::error::Error` trait. This error |
| 17 |
/// only occurs when using convenience routines for building a lazy DFA |
| 18 |
/// directly from a pattern string. |
| 19 |
/// |
| 20 |
/// When the `std` feature is enabled, this implements the `std::error::Error` |
| 21 |
/// trait. |
| 22 |
#[derive(Clone, Debug)] |
| 23 |
pub struct BuildError { |
| 24 |
kind: BuildErrorKind, |
| 25 |
} |
| 26 |
|
| 27 |
#[derive(Clone, Debug)] |
| 28 |
enum BuildErrorKind { |
| 29 |
NFA(nfa::thompson::BuildError), |
| 30 |
InsufficientCacheCapacity { minimum: usize, given: usize }, |
| 31 |
InsufficientStateIDCapacity { err: LazyStateIDError }, |
| 32 |
Unsupported(&'static str), |
| 33 |
} |
| 34 |
|
| 35 |
impl 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")] |
| 67 |
impl 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 |
|
| 76 |
impl 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 |
/// An error that occurs when cache usage has become inefficient. |
| 99 |
/// |
| 100 |
/// One of the weaknesses of a lazy DFA is that it may need to clear its |
| 101 |
/// cache repeatedly if it's not big enough. If this happens too much, then it |
| 102 |
/// can slow searching down significantly. A mitigation to this is to use |
| 103 |
/// heuristics to detect whether the cache is being used efficiently or not. |
| 104 |
/// If not, then a lazy DFA can return a `CacheError`. |
| 105 |
/// |
| 106 |
/// The default configuration of a lazy DFA in this crate is |
| 107 |
/// set such that a `CacheError` will never occur. Instead, |
| 108 |
/// callers must opt into this behavior with settings like |
| 109 |
/// [`dfa::Config::minimum_cache_clear_count`](crate::hybrid::dfa::Config::minimum_cache_clear_count) |
| 110 |
/// and |
| 111 |
/// [`dfa::Config::minimum_bytes_per_state`](crate::hybrid::dfa::Config::minimum_bytes_per_state). |
| 112 |
/// |
| 113 |
/// When the `std` feature is enabled, this implements the `std::error::Error` |
| 114 |
/// trait. |
| 115 |
#[derive(Clone, Debug)] |
| 116 |
pub struct CacheError(()); |
| 117 |
|
| 118 |
impl CacheError { |
| 119 |
pub(crate) fn too_many_cache_clears() -> CacheError { |
| 120 |
CacheError(()) |
| 121 |
} |
| 122 |
|
| 123 |
pub(crate) fn bad_efficiency() -> CacheError { |
| 124 |
CacheError(()) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
#[cfg(feature = "std")] |
| 129 |
impl std::error::Error for CacheError { |
| 130 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 131 |
None |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
impl core::fmt::Display for CacheError { |
| 136 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 137 |
write!(f, "lazy DFA cache has been cleared too many times") |
| 138 |
} |
| 139 |
} |