use crate::types::PacketIdentifier;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum QoS {
AtMostOnce = 0,
AtLeastOnce = 1,
ExactlyOnce = 2,
}
impl From<IdentifiedQoS> for QoS {
fn from(value: IdentifiedQoS) -> Self {
match value {
IdentifiedQoS::AtMostOnce => Self::AtMostOnce,
IdentifiedQoS::AtLeastOnce(_) => Self::AtLeastOnce,
IdentifiedQoS::ExactlyOnce(_) => Self::ExactlyOnce,
}
}
}
impl QoS {
pub(crate) const fn into_bits(self, left_shift: u8) -> u8 {
let bits = match self {
Self::AtMostOnce => 0x00,
Self::AtLeastOnce => 0x01,
Self::ExactlyOnce => 0x02,
};
bits << left_shift
}
pub(crate) const fn try_from_bits(bits: u8) -> Option<Self> {
match bits {
0x00 => Some(Self::AtMostOnce),
0x01 => Some(Self::AtLeastOnce),
0x02 => Some(Self::ExactlyOnce),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IdentifiedQoS {
AtMostOnce,
AtLeastOnce(PacketIdentifier),
ExactlyOnce(PacketIdentifier),
}
impl IdentifiedQoS {
#[inline]
#[must_use]
pub const fn packet_identifier(&self) -> Option<PacketIdentifier> {
match self {
Self::AtMostOnce => None,
Self::AtLeastOnce(pid) | Self::ExactlyOnce(pid) => Some(*pid),
}
}
}