use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CanError {
InvalidIdLength {
max: u32,
got: u32,
},
DataTooLong {
max: usize,
got: usize,
},
InvalidDlc(u8),
FdBrsOnClassic,
RemoteFrameWithData,
BufferTooShort {
need: usize,
have: usize,
},
InvalidTiming(&'static str),
}
impl fmt::Display for CanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::InvalidIdLength { max, got } => {
write!(f, "identifier 0x{got:X} exceeds its width limit 0x{max:X}")
}
Self::DataTooLong { max, got } => {
write!(
f,
"payload of {got} bytes exceeds the {max}-byte maximum for this frame type"
)
}
Self::InvalidDlc(dlc) => {
write!(
f,
"invalid DLC {dlc}: expected 0..=15 (0..=8 for classic frames)"
)
}
Self::FdBrsOnClassic => {
write!(f, "FD-only flag (BRS/ESI) set on a classic CAN frame")
}
Self::RemoteFrameWithData => {
write!(
f,
"remote frame carrying data bytes, or FD frame marked remote"
)
}
Self::BufferTooShort { need, have } => {
write!(f, "buffer of {have} bytes is too short: need {need}")
}
Self::InvalidTiming(reason) => {
write!(f, "invalid bit-timing parameters: {reason}")
}
}
}
}
impl core::error::Error for CanError {}