use std::error::Error;
use std::fmt::{self, Debug, Display, Formatter};
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum StateMachineError<S, E> {
UnknownState {
state: S,
},
UnknownTransition {
source: S,
event: E,
},
CasConflict {
attempts: u32,
},
}
impl<S, E> Display for StateMachineError<S, E>
where
S: Debug,
E: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownState { state } => {
write!(formatter, "unknown state: {state:?}")
}
Self::UnknownTransition { source, event } => {
write!(formatter, "unknown transition: {source:?} --{event:?}--> ?")
}
Self::CasConflict { attempts } => {
write!(
formatter,
"CAS transition failed after {attempts} attempt(s)"
)
}
}
}
}
impl<S, E> Error for StateMachineError<S, E>
where
S: Debug,
E: Debug,
{
}