grep_pcre2/error.rs
1/// An error that can occur in this crate.
2///
3/// Generally, this error corresponds to problems building a regular
4/// expression, whether it's in parsing, compilation or a problem with
5/// guaranteeing a configured optimization.
6#[derive(Clone, Debug)]
7pub struct Error {
8 kind: ErrorKind,
9}
10
11impl Error {
12 pub(crate) fn regex<E: std::error::Error>(err: E) -> Error {
13 Error { kind: ErrorKind::Regex(err.to_string()) }
14 }
15
16 /// Return the kind of this error.
17 pub fn kind(&self) -> &ErrorKind {
18 &self.kind
19 }
20}
21
22/// The kind of an error that can occur.
23#[derive(Clone, Debug)]
24#[non_exhaustive]
25pub enum ErrorKind {
26 /// An error that occurred as a result of parsing a regular expression.
27 /// This can be a syntax error or an error that results from attempting to
28 /// compile a regular expression that is too big.
29 ///
30 /// The string here is the underlying error converted to a string.
31 Regex(String),
32}
33
34impl std::error::Error for Error {
35 fn description(&self) -> &str {
36 match self.kind {
37 ErrorKind::Regex(_) => "regex error",
38 }
39 }
40}
41
42impl std::fmt::Display for Error {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 match self.kind {
45 ErrorKind::Regex(ref s) => write!(f, "{}", s),
46 }
47 }
48}