1use std::error;
2use std::fmt;
3use std::result;
45pub type Result<T> = result::Result<T, Error>;
67/// An error that occurred during the construction of an Aho-Corasick
8/// automaton.
9#[derive(Clone, Debug)]
10pub struct Error {
11 kind: ErrorKind,
12}
1314/// The kind of error that occurred.
15#[derive(Clone, Debug)]
16pub enum ErrorKind {
17/// An error that occurs when constructing an automaton would require the
18 /// use of a state ID that overflows the chosen state ID representation.
19 /// For example, if one is using `u8` for state IDs and builds a DFA with
20 /// 257 states, then the last state's ID will be `256` which cannot be
21 /// represented with `u8`.
22StateIDOverflow {
23/// The maximum possible state ID.
24max: usize,
25 },
26/// An error that occurs when premultiplication of state IDs is requested
27 /// when constructing an Aho-Corasick DFA, but doing so would overflow the
28 /// chosen state ID representation.
29 ///
30 /// When `max == requested_max`, then the state ID would overflow `usize`.
31PremultiplyOverflow {
32/// The maximum possible state id.
33max: usize,
34/// The maximum ID required by premultiplication.
35requested_max: usize,
36 },
37}
3839impl Error {
40/// Return the kind of this error.
41pub fn kind(&self) -> &ErrorKind {
42&self.kind
43 }
4445pub(crate) fn state_id_overflow(max: usize) -> Error {
46 Error { kind: ErrorKind::StateIDOverflow { max } }
47 }
4849pub(crate) fn premultiply_overflow(
50 max: usize,
51 requested_max: usize,
52 ) -> Error {
53 Error { kind: ErrorKind::PremultiplyOverflow { max, requested_max } }
54 }
55}
5657impl error::Error for Error {
58fn description(&self) -> &str {
59match self.kind {
60 ErrorKind::StateIDOverflow { .. } => {
61"state id representation too small"
62}
63 ErrorKind::PremultiplyOverflow { .. } => {
64"state id representation too small for premultiplication"
65}
66 }
67 }
68}
6970impl fmt::Display for Error {
71fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72match self.kind {
73 ErrorKind::StateIDOverflow { max } => write!(
74 f,
75"building the automaton failed because it required \
76 building more states that can be identified, where the \
77 maximum ID for the chosen representation is {}",
78 max,
79 ),
80 ErrorKind::PremultiplyOverflow { max, requested_max } => {
81if max == requested_max {
82write!(
83 f,
84"premultiplication of states requires the ability to \
85 represent a state ID greater than what can fit on \
86 this platform's usize, which is {}",
87 ::std::usize::MAX,
88 )
89 } else {
90write!(
91 f,
92"premultiplication of states requires the ability to \
93 represent at least a state ID of {}, but the chosen \
94 representation only permits a maximum state ID of {}",
95 requested_max, max,
96 )
97 }
98 }
99 }
100 }
101}