/rust/registry/src/index.crates.io-1949cf8c6b5b557f/regex-automata-0.1.10/src/error.rs
Line | Count | Source |
1 | | use std::error; |
2 | | use std::fmt; |
3 | | use std::result; |
4 | | |
5 | | use regex_syntax; |
6 | | |
7 | | pub type Result<T> = result::Result<T, Error>; |
8 | | |
9 | | /// An error that occurred during the construction of a DFA. |
10 | | #[derive(Clone, Debug)] |
11 | | pub struct Error { |
12 | | kind: ErrorKind, |
13 | | } |
14 | | |
15 | | /// The kind of error that occurred. |
16 | | #[derive(Clone, Debug)] |
17 | | pub enum ErrorKind { |
18 | | /// An error that occurred while parsing a regular expression. Note that |
19 | | /// this error may be printed over multiple lines, and is generally |
20 | | /// intended to be end user readable on its own. |
21 | | Syntax(String), |
22 | | /// An error that occurred because an unsupported regex feature was used. |
23 | | /// The message string describes which unsupported feature was used. |
24 | | /// |
25 | | /// The primary regex features that are unsupported are those that require |
26 | | /// look-around, such as the `^` and `$` anchors and the word boundary |
27 | | /// assertion `\b`. These may be supported in the future. |
28 | | Unsupported(String), |
29 | | /// An error that occurred when attempting to serialize a DFA to bytes. |
30 | | Serialize(String), |
31 | | /// An error that occurs when constructing a DFA would require the use of |
32 | | /// a state ID that overflows the chosen state ID representation. For |
33 | | /// example, if one is using `u8` for state IDs and builds a DFA with |
34 | | /// 257 states, then the last state's ID will be `256` which cannot be |
35 | | /// represented with `u8`. |
36 | | /// |
37 | | /// Typically, this error occurs in the determinization process of building |
38 | | /// a DFA (the conversion step from NFA to DFA). It can also occur when |
39 | | /// trying to build a smaller DFA from an existing one. |
40 | | StateIDOverflow { |
41 | | /// The maximum possible state ID. |
42 | | max: usize, |
43 | | }, |
44 | | /// An error that occurs when premultiplication of state IDs is requested, |
45 | | /// but doing so would overflow the chosen state ID representation. |
46 | | /// |
47 | | /// When `max == requested_max`, then the state ID would overflow `usize`. |
48 | | PremultiplyOverflow { |
49 | | /// The maximum possible state id. |
50 | | max: usize, |
51 | | /// The maximum ID required by premultiplication. |
52 | | requested_max: usize, |
53 | | }, |
54 | | } |
55 | | |
56 | | impl Error { |
57 | | /// Return the kind of this error. |
58 | 0 | pub fn kind(&self) -> &ErrorKind { |
59 | 0 | &self.kind |
60 | 0 | } |
61 | | |
62 | 0 | pub(crate) fn syntax(err: regex_syntax::Error) -> Error { |
63 | 0 | Error { kind: ErrorKind::Syntax(err.to_string()) } |
64 | 0 | } |
65 | | |
66 | 0 | pub(crate) fn unsupported_anchor() -> Error { |
67 | 0 | let msg = r"anchors such as ^, $, \A and \z are not supported"; |
68 | 0 | Error { kind: ErrorKind::Unsupported(msg.to_string()) } |
69 | 0 | } |
70 | | |
71 | 0 | pub(crate) fn unsupported_word() -> Error { |
72 | 0 | let msg = r"word boundary assertions (\b and \B) are not supported"; |
73 | 0 | Error { kind: ErrorKind::Unsupported(msg.to_string()) } |
74 | 0 | } |
75 | | |
76 | 0 | pub(crate) fn unsupported_longest_match() -> Error { |
77 | 0 | let msg = "unachored searches with longest match \ |
78 | 0 | semantics are not supported"; |
79 | 0 | Error { kind: ErrorKind::Unsupported(msg.to_string()) } |
80 | 0 | } |
81 | | |
82 | 0 | pub(crate) fn serialize(message: &str) -> Error { |
83 | 0 | Error { kind: ErrorKind::Serialize(message.to_string()) } |
84 | 0 | } |
85 | | |
86 | 0 | pub(crate) fn state_id_overflow(max: usize) -> Error { |
87 | 0 | Error { kind: ErrorKind::StateIDOverflow { max } } |
88 | 0 | } |
89 | | |
90 | 0 | pub(crate) fn premultiply_overflow( |
91 | 0 | max: usize, |
92 | 0 | requested_max: usize, |
93 | 0 | ) -> Error { |
94 | 0 | Error { kind: ErrorKind::PremultiplyOverflow { max, requested_max } } |
95 | 0 | } |
96 | | } |
97 | | |
98 | | impl error::Error for Error { |
99 | 0 | fn description(&self) -> &str { |
100 | 0 | match self.kind { |
101 | 0 | ErrorKind::Syntax(_) => "syntax error", |
102 | 0 | ErrorKind::Unsupported(_) => "unsupported syntax", |
103 | 0 | ErrorKind::Serialize(_) => "serialization error", |
104 | | ErrorKind::StateIDOverflow { .. } => { |
105 | 0 | "state id representation too small" |
106 | | } |
107 | | ErrorKind::PremultiplyOverflow { .. } => { |
108 | 0 | "state id representation too small for premultiplication" |
109 | | } |
110 | | } |
111 | 0 | } |
112 | | } |
113 | | |
114 | | impl fmt::Display for Error { |
115 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
116 | 0 | match self.kind { |
117 | 0 | ErrorKind::Syntax(ref msg) => write!(f, "{}", msg), |
118 | 0 | ErrorKind::Unsupported(ref msg) => write!(f, "{}", msg), |
119 | 0 | ErrorKind::Serialize(ref msg) => { |
120 | 0 | write!(f, "DFA serialization error: {}", msg) |
121 | | } |
122 | 0 | ErrorKind::StateIDOverflow { max } => write!( |
123 | 0 | f, |
124 | 0 | "building the DFA failed because it required building \ |
125 | 0 | more states that can be identified, where the maximum \ |
126 | 0 | ID for the chosen representation is {}", |
127 | | max, |
128 | | ), |
129 | 0 | ErrorKind::PremultiplyOverflow { max, requested_max } => { |
130 | 0 | if max == requested_max { |
131 | 0 | write!( |
132 | 0 | f, |
133 | 0 | "premultiplication of states requires the ability to \ |
134 | 0 | represent a state ID greater than what can fit on \ |
135 | 0 | this platform's usize, which is {}", |
136 | | ::std::usize::MAX, |
137 | | ) |
138 | | } else { |
139 | 0 | write!( |
140 | 0 | f, |
141 | 0 | "premultiplication of states requires the ability to \ |
142 | 0 | represent at least a state ID of {}, but the chosen \ |
143 | 0 | representation only permits a maximum state ID of {}", |
144 | | requested_max, max, |
145 | | ) |
146 | | } |
147 | | } |
148 | | } |
149 | 0 | } |
150 | | } |