Skip to main content

serde_duration_ext/
error.rs

1use std::error::Error as StdError;
2use std::fmt::{Display, Formatter, Result as FmtResult};
3
4#[derive(Debug, PartialEq, Eq, Clone)]
5pub enum Error {
6    Syntax(String),
7    UnitNotSupported(String),
8    NoUnitProvided,
9    NoValueProvided,
10    StringDoesNotMatchRegex,
11}
12
13impl StdError for Error {}
14
15impl Display for Error {
16    fn fmt(&self, f: &mut Formatter) -> FmtResult {
17        match self {
18            Error::Syntax(err) => err.fmt(f),
19            Error::UnitNotSupported(err) => err.fmt(f),
20            Error::NoUnitProvided => f.write_str("No unit provided"),
21            Error::NoValueProvided => f.write_str("No value provided"),
22            Error::StringDoesNotMatchRegex => f.write_str("String does not match regex"),
23        }
24    }
25}
26
27impl serde::de::Error for Error {
28    fn custom<T: Display>(msg: T) -> Self {
29        Error::Syntax(msg.to_string())
30    }
31}