use crate::error::{ErrCode, Error};
use std::fmt;
macro_rules! dep_error {
($error:ty, $kind:expr, $code:expr, $reason:expr) => {
impl From<$error> for Error {
#[must_use]
fn from(inner: $error) -> Self {
Self::new($code, $reason, Some($kind(inner)))
}
}
};
}
dep_error!(
std::env::VarError,
ErrSource::Var,
ErrCode::Env,
"There was an error processing your enviroment"
);
dep_error!(
std::io::Error,
ErrSource::Io,
ErrCode::Io,
"There was an error processing your request"
);
dep_error!(
toml::de::Error,
ErrSource::TomlDe,
ErrCode::Parse,
"There was an error deserializing TOML"
);
dep_error!(
toml::ser::Error,
ErrSource::TomlSer,
ErrCode::Parse,
"There was an error serializing TOML"
);
#[derive(Debug)]
#[allow(clippy::large_enum_variant, variant_size_differences)]
pub(crate) enum ErrSource {
Io(std::io::Error),
TomlDe(toml::de::Error),
TomlSer(toml::ser::Error),
Var(std::env::VarError),
}
impl std::error::Error for ErrSource {}
impl fmt::Display for ErrSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(source) => write!(f, "{source}"),
Self::TomlDe(source) => write!(f, "{source}"),
Self::TomlSer(source) => write!(f, "{source}"),
Self::Var(source) => write!(f, "{source}"),
}
}
}