Skip to main content

can_hal_socketcan/
error.rs

1use std::fmt;
2use std::io;
3
4/// Errors from the SocketCAN backend.
5#[derive(Debug)]
6pub enum SocketCanError {
7    /// An I/O error from the underlying socket.
8    Io(io::Error),
9    /// Failed to construct a frame (invalid ID, data length, etc.).
10    InvalidFrame(String),
11    /// The interface index or name is invalid.
12    InvalidInterface(String),
13}
14
15impl fmt::Display for SocketCanError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            Self::Io(e) => write!(f, "SocketCAN I/O error: {e}"),
19            Self::InvalidFrame(msg) => write!(f, "invalid frame: {msg}"),
20            Self::InvalidInterface(msg) => write!(f, "invalid interface: {msg}"),
21        }
22    }
23}
24
25impl std::error::Error for SocketCanError {
26    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
27        match self {
28            Self::Io(e) => Some(e),
29            _ => None,
30        }
31    }
32}
33
34impl From<io::Error> for SocketCanError {
35    fn from(e: io::Error) -> Self {
36        Self::Io(e)
37    }
38}