mini_slcan/
identifier.rs

1//! Defines CAN identifier types.
2
3use core::fmt;
4use defmt::Format;
5
6/// Standard 11-bit CAN identifier.
7#[derive(Copy, Clone, Eq, PartialEq, Format)]
8pub struct Identifier(u16);
9
10impl Identifier {
11    pub fn from_raw(raw: u16) -> Option<Self> {
12        if raw > 0x7FF {
13            None
14        } else {
15            Some(Self(raw))
16        }
17    }
18
19    pub fn as_raw(&self) -> u16 {
20        self.0
21    }
22}
23
24impl fmt::Debug for Identifier {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "0x{:03X}", self.0)
27    }
28}
29
30/// Extended 29-bit identifier.
31#[derive(Copy, Clone, Eq, PartialEq, Format)]
32pub struct ExtIdentifier(u32);
33
34impl ExtIdentifier {
35    pub fn from_raw(raw: u32) -> Option<Self> {
36        if raw > 0x1FFFFFFF {
37            None
38        } else {
39            Some(Self(raw))
40        }
41    }
42
43    pub fn as_raw(&self) -> u32 {
44        self.0
45    }
46}
47
48impl fmt::Debug for ExtIdentifier {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        write!(f, "0x{:08X}", self.0)
51    }
52}