mctp_rs/
endpoint_id.rs

1use bit_register::{NumBytes, TryFromBits, TryIntoBits};
2
3#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
4#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5pub enum EndpointId {
6    /// 0x00
7    #[default]
8    Null,
9    /// 0xFF
10    Broadcast,
11    /// 0x08 - 0x7F
12    Id(u8),
13}
14
15impl TryFrom<u8> for EndpointId {
16    type Error = &'static str;
17    fn try_from(value: u8) -> Result<Self, Self::Error> {
18        Self::try_from_bits(value as u32)
19    }
20}
21
22impl TryFromBits<u32> for EndpointId {
23    fn try_from_bits(bits: u32) -> Result<Self, &'static str> {
24        match bits {
25            0x00 => Ok(EndpointId::Null),
26            0xFF => Ok(EndpointId::Broadcast),
27            0x08..=0xFE => Ok(EndpointId::Id(bits as u8)),
28            _ => Err("Invalid endpoint ID"),
29        }
30    }
31}
32
33impl TryIntoBits<u32> for EndpointId {
34    fn try_into_bits(self) -> Result<u32, &'static str> {
35        match self {
36            EndpointId::Null => Ok(0x00),
37            EndpointId::Broadcast => Ok(0xFF),
38            EndpointId::Id(id) => Ok(id as u32),
39        }
40    }
41}
42
43impl NumBytes for EndpointId {
44    const NUM_BYTES: usize = 1;
45}