use core::fmt;
pub(crate) type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug)]
pub struct Error(ErrorKind);
#[derive(Debug)]
pub(crate) enum ErrorKind {
Regex(regex::Error),
Format(String),
Parse(String),
}
impl Error {
pub(crate) fn new(e: impl Into<ErrorKind>) -> Self {
Self(e.into())
}
pub(crate) fn format(e: impl Into<String>) -> Self {
Self(ErrorKind::Format(e.into()))
}
pub(crate) fn parse(e: impl Into<String>) -> Self {
Self(ErrorKind::Parse(e.into()))
}
#[must_use]
pub fn is_format(&self) -> bool {
matches!(self.0, ErrorKind::Format(..) | ErrorKind::Regex(..))
}
#[must_use]
pub fn is_parse(&self) -> bool {
matches!(self.0, ErrorKind::Parse(..))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
ErrorKind::Regex(e) => fmt::Display::fmt(e, f),
ErrorKind::Format(e) | ErrorKind::Parse(e) => fmt::Display::fmt(e, f),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.0 {
ErrorKind::Regex(e) => Some(e),
_ => None,
}
}
}
impl From<regex::Error> for ErrorKind {
fn from(e: regex::Error) -> Self {
Self::Regex(e)
}
}