use core::fmt;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum NetworkPriority {
#[default]
Normal = 0,
Urgent = 1,
CriticalEquipment = 2,
LifeSafety = 3,
}
impl NetworkPriority {
pub const ALL: [Self; 4] = [
Self::Normal,
Self::Urgent,
Self::CriticalEquipment,
Self::LifeSafety,
];
#[must_use]
pub const fn from_control(control: u8) -> Self {
match control & 0x03 {
0 => Self::Normal,
1 => Self::Urgent,
2 => Self::CriticalEquipment,
_ => Self::LifeSafety,
}
}
#[must_use]
pub const fn to_raw(self) -> u8 {
self as u8
}
}
impl TryFrom<u8> for NetworkPriority {
type Error = PriorityError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
if value > 3 {
return Err(PriorityError { value });
}
Ok(Self::from_control(value))
}
}
impl From<NetworkPriority> for u8 {
fn from(priority: NetworkPriority) -> Self {
priority.to_raw()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub struct PriorityError {
pub value: u8,
}
impl fmt::Display for PriorityError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "network priority {} is not 0 to 3", self.value)
}
}
impl core::error::Error for PriorityError {}