1use std::fmt::{Display, Formatter};
2
3pub type Result<T> = std::result::Result<T, CoreError>;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum CoreError {
9 Validation(String),
11 NotFound(String),
13 IdentityMismatch(String),
15 IllegalTransition(String),
17 CommandReuse(String),
19 Causal(String),
21 Encoding(String),
23}
24
25impl CoreError {
26 pub const fn code(&self) -> &'static str {
28 match self {
29 Self::Validation(_) => "validation_failed",
30 Self::NotFound(_) => "not_found",
31 Self::IdentityMismatch(_) => "identity_mismatch",
32 Self::IllegalTransition(_) => "illegal_transition",
33 Self::CommandReuse(_) => "command_id_reused",
34 Self::Causal(_) => "causal_error",
35 Self::Encoding(_) => "encoding_failed",
36 }
37 }
38}
39
40impl Display for CoreError {
41 fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
42 let message = match self {
43 Self::Validation(message)
44 | Self::NotFound(message)
45 | Self::IdentityMismatch(message)
46 | Self::IllegalTransition(message)
47 | Self::CommandReuse(message)
48 | Self::Causal(message)
49 | Self::Encoding(message) => message,
50 };
51 write!(formatter, "{}: {message}", self.code())
52 }
53}
54
55impl std::error::Error for CoreError {}
56
57impl From<serde_json::Error> for CoreError {
58 fn from(error: serde_json::Error) -> Self {
59 Self::Encoding(error.to_string())
60 }
61}