de_regex/
error.rs

1use std::fmt::{Display, Formatter};
2
3/// An error that occurred during deserialization.
4#[derive(Debug)]
5pub enum Error {
6    /// An error occurred while parsing the regular expression
7    BadRegex(regex::Error),
8
9    /// The string doesn't match the pattern
10    NoMatch(),
11
12    /// A value couldn't be parsed into the required type
13    BadValue {
14        /// The name of the group
15        name: String,
16
17        /// The value that couldn't be converted to the target value
18        value: String,
19    },
20
21    /// Some other deserialization/serde related error
22    Custom(String),
23}
24
25impl serde::de::Error for Error {
26    fn custom<T>(msg: T) -> Self where T: Display {
27        Self::Custom(msg.to_string())
28    }
29}
30
31impl std::error::Error for Error {}
32
33impl Display for Error {
34    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35        use Error::*;
36        match self {
37            BadRegex(err) => err.fmt(f),
38            NoMatch() => write!(f, "String doesn't match pattern"),
39            BadValue { name, value } => write!(f, "Unable to convert value for group {}: {}", name, value),
40            Custom(err) => write!(f, "{}", err),
41        }
42    }
43}
44
45// Do not use this alias in public parts of the crate because
46// it would hide the direct link to the actual error type in rustdoc.
47pub(crate) type Result<T> = std::result::Result<T, Error>;