1 use std::error; 2 use std::fmt; 3 use std::result; 4 5 pub type Result<T> = result::Result<T, Error>; 6 7 /// An error that occurred during the construction of an Aho-Corasick 8 /// automaton. 9 #[derive(Clone, Debug)] 10 pub struct Error { 11 kind: ErrorKind, 12 } 13 14 /// The kind of error that occurred. 15 #[derive(Clone, Debug)] 16 pub 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`. 22 StateIDOverflow { 23 /// The maximum possible state ID. 24 max: 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`. 31 PremultiplyOverflow { 32 /// The maximum possible state id. 33 max: usize, 34 /// The maximum ID required by premultiplication. 35 requested_max: usize, 36 }, 37 } 38 39 impl Error { 40 /// Return the kind of this error. kind(&self) -> &ErrorKind41 pub fn kind(&self) -> &ErrorKind { 42 &self.kind 43 } 44 state_id_overflow(max: usize) -> Error45 pub(crate) fn state_id_overflow(max: usize) -> Error { 46 Error { kind: ErrorKind::StateIDOverflow { max } } 47 } 48 premultiply_overflow( max: usize, requested_max: usize, ) -> Error49 pub(crate) fn premultiply_overflow( 50 max: usize, 51 requested_max: usize, 52 ) -> Error { 53 Error { kind: ErrorKind::PremultiplyOverflow { max, requested_max } } 54 } 55 } 56 57 impl error::Error for Error { description(&self) -> &str58 fn description(&self) -> &str { 59 match 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 } 69 70 impl fmt::Display for Error { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 72 match 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 } => { 81 if max == requested_max { 82 write!( 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 { 90 write!( 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 } 102