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