veecle_os_data_support_can/
error.rs

1use core::error::Error;
2use core::fmt::Display;
3
4#[derive(Debug)]
5#[non_exhaustive]
6/// Error decoding a CAN frame.
7pub enum CanDecodeError {
8    /// The frame given to deserialize from had the wrong id.
9    IncorrectId,
10
11    /// The frame given to deserialize from had the wrong amount of data.
12    IncorrectBufferSize,
13
14    /// Value was out of range.
15    OutOfRange {
16        /// Name of the field.
17        name: &'static str,
18        /// Type of the field.
19        ty: &'static str,
20        /// Additional details (including the expected range).
21        message: &'static str,
22    },
23
24    /// Validation failure.
25    Invalid {
26        /// Additional details about what was invalid.
27        message: &'static str,
28    },
29}
30
31impl CanDecodeError {
32    /// Create an instance of [`Self::Invalid`].
33    pub fn invalid(message: &'static str) -> Self {
34        Self::Invalid { message }
35    }
36}
37
38impl Display for CanDecodeError {
39    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
40        match *self {
41            CanDecodeError::IncorrectId => write!(f, "incorrect CAN frame id"),
42            CanDecodeError::IncorrectBufferSize => write!(f, "incorrect CAN frame size"),
43            CanDecodeError::OutOfRange { name, ty, message } => {
44                write!(f, "field {name}:{ty}: {message}")
45            }
46            CanDecodeError::Invalid { message } => write!(f, "validation failure: {message}"),
47        }
48    }
49}
50
51impl Error for CanDecodeError {}