fst_regex/error.rs
1use std::error;
2use std::fmt;
3
4use regex_syntax;
5
6/// An error that occurred while compiling a regular expression.
7#[derive(Debug)]
8pub enum Error {
9 /// A problem with the syntax of a regular expression.
10 Syntax(regex_syntax::Error),
11 /// Too many instructions resulting from the regular expression.
12 ///
13 /// The number given is the limit that was exceeded.
14 CompiledTooBig(usize),
15 /// Too many automata states resulting from the regular expression.
16 ///
17 /// This is distinct from `CompiledTooBig` because `TooManyStates` refers
18 /// to the DFA construction where as `CompiledTooBig` refers to the NFA
19 /// construction.
20 ///
21 /// The number given is the limit that was exceeded.
22 TooManyStates(usize),
23 /// Lazy quantifiers are not allowed (because they have no useful
24 /// interpretation when used purely for automata intersection, as is the
25 /// case in this crate).
26 NoLazy,
27 /// Word boundaries are currently not allowed.
28 ///
29 /// This restriction may be lifted in the future.
30 NoWordBoundary,
31 /// Empty or "zero width assertions" such as `^` or `$` are currently
32 /// not allowed.
33 ///
34 /// This restriction may be lifted in the future.
35 NoEmpty,
36 /// Byte literals such as `(?-u:\xff)` are not allowed.
37 ///
38 /// This restriction may be lifted in the future.
39 NoBytes,
40}
41
42impl From<regex_syntax::Error> for Error {
43 #[inline]
44 fn from(err: regex_syntax::Error) -> Error {
45 Error::Syntax(err)
46 }
47}
48
49impl fmt::Display for Error {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 use self::Error::*;
52 match *self {
53 Syntax(ref err) => err.fmt(f),
54 CompiledTooBig(size_limit) => write!(
55 f,
56 "Compiled regex exceeds size limit of {} bytes",
57 size_limit
58 ),
59 TooManyStates(size_limit) => write!(
60 f,
61 "Compiled regex exceeds size limit of {} states",
62 size_limit
63 ),
64 NoLazy => write!(
65 f,
66 "Lazy reptition operators such as '+?' are \
67 not allowed."
68 ),
69 NoWordBoundary => write!(
70 f,
71 "Word boundary operators are not \
72 allowed."
73 ),
74 NoEmpty => write!(
75 f,
76 "Empty match operators are not allowed \
77 (hopefully temporary)."
78 ),
79 NoBytes => write!(f, "Byte literals are not allowed."),
80 }
81 }
82}
83
84impl error::Error for Error {
85 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
86 use self::Error::*;
87 match *self {
88 Syntax(ref err) => Some(err),
89 _ => None,
90 }
91 }
92}