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("can frame data exceeds the maximum length"),
18            CanError::InvalidFdLength => {
19                f.write_str("can-fd data length is not a valid frame length")
20            }
21        }
22    }
23}
24
25// `core::error::Error` rather than `std::error::Error`, so a caller on a
26// microcontroller gets the same trait a caller on a gateway does.
27impl core::error::Error for CanError {}