Skip to main content

pamoja_can/
error.rs

1//! The error type for CAN framing.
2
3/// What can go wrong building a CAN frame.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum CanError {
6    /// The data is longer than the frame kind allows (8 bytes for classic CAN, 64 for
7    /// CAN-FD).
8    DataTooLong,
9    /// The data length is not one a CAN-FD frame can carry. CAN-FD allows 0 to 8 bytes,
10    /// then only 12, 16, 20, 24, 32, 48, and 64.
11    InvalidFdLength,
12}
13
14impl core::fmt::Display for CanError {
15    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16        match self {
17            CanError::DataTooLong => f.write_str(
18                "the data is longer than the frame carries: 8 bytes for classic CAN, 64 for CAN FD",
19            ),
20            CanError::InvalidFdLength => {
21                f.write_str("CAN FD carries 0 to 8 bytes, then only 12, 16, 20, 24, 32, 48, or 64")
22            }
23        }
24    }
25}
26
27// `core::error::Error` rather than `std::error::Error`, so a caller on a
28// microcontroller gets the same trait a caller on a gateway does.
29impl core::error::Error for CanError {}