1use std::fmt;
2
3pub type Result<T> = ::std::result::Result<T, Error>;
5
6pub type ParseErrorPosition = usize;
7
8#[derive(Debug)]
10pub enum Error {
11 ParseError(ParseErrorPosition, ParseError),
13 CompileError(CompileError),
15 RuntimeError(RuntimeError),
17
18 #[doc(hidden)]
21 __Nonexhaustive,
22}
23
24#[derive(Debug)]
26pub enum ParseError {
27 GeneralParseError(String),
29 UnclosedOpenParen,
31 InvalidRepeat,
33 RecursionExceeded,
35 TrailingBackslash,
37 InvalidEscape(String),
39 UnclosedUnicodeName,
41 InvalidHex,
43 InvalidCodepointValue,
45 InvalidClass,
47 UnknownFlag(String),
49 NonUnicodeUnsupported,
51 InvalidBackref,
53 TargetNotRepeatable,
55 InvalidGroupName,
57 InvalidGroupNameBackref(String),
59
60 #[doc(hidden)]
63 __Nonexhaustive,
64}
65
66#[derive(Debug)]
68pub enum CompileError {
69 InnerError(regex::Error),
71 LookBehindNotConst,
73 InvalidGroupName,
75 InvalidGroupNameBackref(String),
77 InvalidBackref,
79 NamedBackrefOnly,
81
82 #[doc(hidden)]
85 __Nonexhaustive,
86}
87
88#[derive(Debug)]
90pub enum RuntimeError {
91 StackOverflow,
93 BacktrackLimitExceeded,
97
98 #[doc(hidden)]
101 __Nonexhaustive,
102}
103
104impl ::std::error::Error for Error {}
105
106impl fmt::Display for ParseError {
107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108 match self {
109 ParseError::GeneralParseError(s) => write!(f, "General parsing error: {}", s),
110 ParseError::UnclosedOpenParen => {
111 write!(f, "Opening parenthesis without closing parenthesis")
112 }
113 ParseError::InvalidRepeat => write!(f, "Invalid repeat syntax"),
114 ParseError::RecursionExceeded => write!(f, "Pattern too deeply nested"),
115 ParseError::TrailingBackslash => write!(f, "Backslash without following character"),
116 ParseError::InvalidEscape(s) => write!(f, "Invalid escape: {}", s),
117 ParseError::UnclosedUnicodeName => write!(f, "Unicode escape not closed"),
118 ParseError::InvalidHex => write!(f, "Invalid hex escape"),
119 ParseError::InvalidCodepointValue => {
120 write!(f, "Invalid codepoint for hex or unicode escape")
121 }
122 ParseError::InvalidClass => write!(f, "Invalid character class"),
123 ParseError::UnknownFlag(s) => write!(f, "Unknown group flag: {}", s),
124 ParseError::NonUnicodeUnsupported => write!(f, "Disabling Unicode not supported"),
125 ParseError::InvalidBackref => write!(f, "Invalid back reference"),
126 ParseError::InvalidGroupName => write!(f, "Could not parse group name"),
127 ParseError::InvalidGroupNameBackref(s) => {
128 write!(f, "Invalid group name in back reference: {}", s)
129 }
130 ParseError::TargetNotRepeatable => write!(f, "Target of repeat operator is invalid"),
131
132 ParseError::__Nonexhaustive => unreachable!(),
133 }
134 }
135}
136
137impl fmt::Display for CompileError {
138 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 match self {
140 CompileError::InnerError(e) => write!(f, "Regex error: {}", e),
141 CompileError::LookBehindNotConst => {
142 write!(f, "Look-behind assertion without constant size")
143 },
144 CompileError::InvalidGroupName => write!(f, "Could not parse group name"),
145 CompileError::InvalidGroupNameBackref(s) => write!(f, "Invalid group name in back reference: {}", s),
146 CompileError::InvalidBackref => write!(f, "Invalid back reference"),
147 CompileError::NamedBackrefOnly => write!(f, "Numbered backref/call not allowed because named group was used, use a named backref instead"),
148
149 CompileError::__Nonexhaustive => unreachable!(),
150 }
151 }
152}
153
154impl fmt::Display for RuntimeError {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156 match self {
157 RuntimeError::StackOverflow => write!(f, "Max stack size exceeded for backtracking"),
158 RuntimeError::BacktrackLimitExceeded => {
159 write!(f, "Max limit for backtracking count exceeded")
160 }
161
162 RuntimeError::__Nonexhaustive => unreachable!(),
163 }
164 }
165}
166
167impl fmt::Display for Error {
168 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169 match self {
170 Error::ParseError(position, parse_error) => {
171 write!(f, "Parsing error at position {}: {}", position, parse_error)
172 }
173 Error::CompileError(compile_error) => {
174 write!(f, "Error compiling regex: {}", compile_error)
175 }
176 Error::RuntimeError(runtime_error) => {
177 write!(f, "Error executing regex: {}", runtime_error)
178 }
179
180 Error::__Nonexhaustive => unreachable!(),
181 }
182 }
183}
184
185impl From<CompileError> for Error {
186 fn from(compile_error: CompileError) -> Self {
187 Error::CompileError(compile_error)
188 }
189}