use std::fmt::{Display, Formatter};
pub type Result<T> = std::result::Result<T, CoreError>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CoreError {
Validation(String),
NotFound(String),
IdentityMismatch(String),
IllegalTransition(String),
CommandReuse(String),
Causal(String),
Encoding(String),
}
impl CoreError {
pub const fn code(&self) -> &'static str {
match self {
Self::Validation(_) => "validation_failed",
Self::NotFound(_) => "not_found",
Self::IdentityMismatch(_) => "identity_mismatch",
Self::IllegalTransition(_) => "illegal_transition",
Self::CommandReuse(_) => "command_id_reused",
Self::Causal(_) => "causal_error",
Self::Encoding(_) => "encoding_failed",
}
}
}
impl Display for CoreError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
let message = match self {
Self::Validation(message)
| Self::NotFound(message)
| Self::IdentityMismatch(message)
| Self::IllegalTransition(message)
| Self::CommandReuse(message)
| Self::Causal(message)
| Self::Encoding(message) => message,
};
write!(formatter, "{}: {message}", self.code())
}
}
impl std::error::Error for CoreError {}
impl From<serde_json::Error> for CoreError {
fn from(error: serde_json::Error) -> Self {
Self::Encoding(error.to_string())
}
}