fancy_regex_fork_pb/
error.rs

1use std::fmt;
2
3/// Result type for this crate with specific error enum.
4pub type Result<T> = ::std::result::Result<T, Error>;
5
6/// An error for the result of compiling or running a regex.
7#[derive(Debug, PartialEq)]
8pub enum Error {
9    // Compile time errors
10    /// General parsing error
11    ParseError,
12    /// Opening parenthesis without closing parenthesis, e.g. `(a|b`
13    UnclosedOpenParen,
14    /// Invalid repeat syntax
15    InvalidRepeat,
16    /// Pattern too deeply nested
17    RecursionExceeded,
18    /// Look-behind assertion without constant size
19    LookBehindNotConst,
20    /// Backslash without following character
21    TrailingBackslash,
22    /// Invalid escape
23    InvalidEscape,
24    /// Unicode escape not closed
25    UnclosedUnicodeName,
26    /// Invalid hex escape
27    InvalidHex,
28    /// Invalid codepoint for hex or unicode escape
29    InvalidCodepointValue,
30    /// Invalid character class
31    InvalidClass,
32    /// Unknown group flag
33    UnknownFlag,
34    /// Disabling Unicode not supported
35    NonUnicodeUnsupported,
36    /// Invalid back reference
37    InvalidBackref,
38    /// Regex crate error
39    InnerError(regex::Error),
40
41    // Run time errors
42    /// Max stack size exceeded for backtracking while executing regex.
43    StackOverflow,
44    /// Max limit for backtracking count exceeded while executing the regex.
45    /// Configure using
46    /// [`RegexBuilder::backtrack_limit`](struct.RegexBuilder.html#method.backtrack_limit).
47    BacktrackLimitExceeded,
48
49    /// This enum may grow additional variants, so this makes sure clients don't count on exhaustive
50    /// matching. Otherwise, adding a new variant could break existing code.
51    #[doc(hidden)]
52    __Nonexhaustive,
53}
54
55impl ::std::error::Error for Error {}
56
57impl fmt::Display for Error {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        // We should make these more helpful, e.g. by including the parts of the regex that lead to
60        // the error.
61        match self {
62            Error::ParseError => write!(f, "General parsing error"),
63            Error::UnclosedOpenParen => {
64                write!(f, "Opening parenthesis without closing parenthesis")
65            }
66            Error::InvalidRepeat => write!(f, "Invalid repeat syntax"),
67            Error::RecursionExceeded => write!(f, "Pattern too deeply nested"),
68            Error::LookBehindNotConst => write!(f, "Look-behind assertion without constant size"),
69            Error::TrailingBackslash => write!(f, "Backslash without following character"),
70            Error::InvalidEscape => write!(f, "Invalid escape"),
71            Error::UnclosedUnicodeName => write!(f, "Unicode escape not closed"),
72            Error::InvalidHex => write!(f, "Invalid hex escape"),
73            Error::InvalidCodepointValue => {
74                write!(f, "Invalid codepoint for hex or unicode escape")
75            }
76            Error::InvalidClass => write!(f, "Invalid character class"),
77            Error::UnknownFlag => write!(f, "Unknown group flag"),
78            Error::NonUnicodeUnsupported => write!(f, "Disabling Unicode not supported"),
79            Error::InvalidBackref => write!(f, "Invalid back reference"),
80            Error::InnerError(e) => write!(f, "Regex error: {}", e),
81            Error::StackOverflow => write!(f, "Max stack size exceeded for backtracking"),
82            Error::BacktrackLimitExceeded => write!(f, "Max limit for backtracking count exceeded"),
83            Error::__Nonexhaustive => unreachable!(),
84        }
85    }
86}