1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Defines CAN identifier types.

use core::fmt;
use defmt::Format;

/// Standard 11-bit CAN identifier.
#[derive(Copy, Clone, Eq, PartialEq, Format)]
pub struct Identifier(u16);

impl Identifier {
    pub fn from_raw(raw: u16) -> Option<Self> {
        if raw > 0x7FF {
            None
        } else {
            Some(Self(raw))
        }
    }

    pub fn as_raw(&self) -> u16 {
        self.0
    }
}

impl fmt::Debug for Identifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:03X}", self.0)
    }
}

/// Extended 29-bit identifier.
#[derive(Copy, Clone, Eq, PartialEq, Format)]
pub struct ExtIdentifier(u32);

impl ExtIdentifier {
    pub fn from_raw(raw: u32) -> Option<Self> {
        if raw > 0x1FFFFFFF {
            None
        } else {
            Some(Self(raw))
        }
    }

    pub fn as_raw(&self) -> u32 {
        self.0
    }
}

impl fmt::Debug for ExtIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:08X}", self.0)
    }
}