use std::fmt;
use std::io;
use crate::protocol::word::WordCategory;
use crate::protocol::CommandResponse;
use crate::protocol::TrapResponse;
pub type DeviceResult<T> = Result<T, DeviceError>;
#[derive(Debug, Clone)]
pub enum DeviceError {
Connection(io::ErrorKind),
Authentication {
response: TrapResponse,
},
Channel {
message: String,
},
ResponseSequence {
received: CommandResponse,
expected: Vec<WordCategory>,
},
}
impl fmt::Display for DeviceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DeviceError::Connection(err) => write!(f, "Connection error: {}", err),
DeviceError::Authentication { response } => {
write!(f, "Authentication failed: {}", response)
}
DeviceError::Channel { message } => write!(f, "Channel error: {}", message),
DeviceError::ResponseSequence { received, expected } => write!(
f,
"Unexpected response sequence: received {:?}, expected {:?}",
received, expected
),
}
}
}
impl From<io::Error> for DeviceError {
fn from(error: io::Error) -> Self {
DeviceError::Connection(error.kind())
}
}
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for DeviceError {
fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
DeviceError::Channel {
message: "Failed to send message through channel".to_string(),
}
}
}
impl std::error::Error for DeviceError {}