macro_rules! ErrorKind {
($(
($kind:ident, $ctor:ident)
),* $(,)?) => {
#[derive(Debug, Clone, PartialEq)]
pub enum ErrorKind {
$(
$kind,
)*
}
impl std::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
$(
Self::$kind => write!(f, "{}", stringify!($kind)),
)*
}
}
}
impl Error {
$(
#[doc = concat!(
"Creates a new [`Error`] with the `",
stringify!($kind),
"` kind and the given description."
)]
pub(crate) fn $ctor(desc: impl Into<String>) -> crate::error::Error {
Self {
kind: ErrorKind::$kind,
desc: desc.into(),
}
}
)*
pub fn kind(&self) -> ErrorKind {
self.kind.clone()
}
}
};
}
ErrorKind!(
(ComponentGraphError, component_graph_error),
(ConnectionFailure, connection_failure),
(ChronoError, chrono_error),
(DroppedUnusedFormulas, dropped_unused_formulas),
(FormulaEngineError, formula_engine_error),
(Internal, internal),
(APIServerError, api_server_error),
);
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
kind: ErrorKind,
desc: String,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.kind, self.desc)
}
}
impl std::error::Error for Error {}