use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum BusState {
Created = 0,
Starting = 1,
Running = 2,
Stopping = 3,
Stopped = 4,
}
impl BusState {
pub fn accepts_messages(&self) -> bool {
matches!(self, Self::Running)
}
pub fn can_start(&self) -> bool {
matches!(self, Self::Created | Self::Stopped)
}
pub fn can_stop(&self) -> bool {
matches!(self, Self::Running | Self::Starting)
}
}
impl fmt::Display for BusState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Created => write!(f, "CREATED"),
Self::Starting => write!(f, "STARTING"),
Self::Running => write!(f, "RUNNING"),
Self::Stopping => write!(f, "STOPPING"),
Self::Stopped => write!(f, "STOPPED"),
}
}
}
impl TryFrom<u8> for BusState {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Created),
1 => Ok(Self::Starting),
2 => Ok(Self::Running),
3 => Ok(Self::Stopping),
4 => Ok(Self::Stopped),
_ => Err(()),
}
}
}