use std::error::Error;
use std::fmt;
use std::io;
#[derive(Debug)]
pub enum AutomotiveError {
CanError(String),
CanFdError(String),
IsoTpError(String),
J1939Error(String),
UdsError(String),
ObdError(String),
DoIPError(String),
ConnectionFailed,
SendFailed,
ReceiveFailed,
Timeout,
BufferOverflow,
InvalidParameter,
NotInitialized,
PortError(String),
InvalidData,
InvalidChecksum,
IoError(io::Error),
ChecksumError,
}
impl fmt::Display for AutomotiveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AutomotiveError::CanError(msg) => write!(f, "CAN error: {}", msg),
AutomotiveError::CanFdError(msg) => write!(f, "CAN FD error: {}", msg),
AutomotiveError::IsoTpError(msg) => write!(f, "ISO-TP error: {}", msg),
AutomotiveError::J1939Error(msg) => write!(f, "J1939 error: {}", msg),
AutomotiveError::UdsError(msg) => write!(f, "UDS error: {}", msg),
AutomotiveError::ObdError(msg) => write!(f, "OBD error: {}", msg),
AutomotiveError::DoIPError(msg) => write!(f, "DoIP error: {}", msg),
AutomotiveError::ConnectionFailed => write!(f, "DoIP connection failed"),
AutomotiveError::SendFailed => write!(f, "DoIP send failed"),
AutomotiveError::ReceiveFailed => write!(f, "DoIP receive failed"),
AutomotiveError::Timeout => write!(f, "Operation timed out"),
AutomotiveError::BufferOverflow => write!(f, "Buffer overflow"),
AutomotiveError::InvalidParameter => write!(f, "Invalid parameter"),
AutomotiveError::NotInitialized => write!(f, "Component not initialized"),
AutomotiveError::PortError(msg) => write!(f, "Port error: {}", msg),
AutomotiveError::InvalidData => write!(f, "Invalid data received"),
AutomotiveError::InvalidChecksum => write!(f, "Invalid checksum"),
AutomotiveError::IoError(err) => write!(f, "I/O error: {}", err),
AutomotiveError::ChecksumError => write!(f, "Checksum error"),
}
}
}
impl Error for AutomotiveError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
AutomotiveError::IoError(err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for AutomotiveError {
fn from(err: io::Error) -> Self {
AutomotiveError::IoError(err)
}
}
pub type Result<T> = std::result::Result<T, AutomotiveError>;