use core::fmt::Display;
use std::sync::Arc;
pub use mtp_spec::error::*;
pub type Result<T> = crate::error::Result<T, Arc<UsbError>>;
pub type Error = crate::error::Error<Arc<UsbError>>;
impl From<Arc<UsbError>> for Error {
fn from(error: Arc<UsbError>) -> Self {
Self::Core(MtpError::Transport(error))
}
}
#[derive(Debug)]
pub enum UsbError {
NoApplicableInterface,
Native(nusb::Error),
Transfer(nusb::transfer::TransferError),
NoData,
Timeout,
TooMuchData,
}
impl Display for UsbError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NoApplicableInterface => write!(f, "No applicable interface found"),
Self::Native(error) => write!(f, "{error}"),
Self::Transfer(error) => write!(f, "{error}"),
Self::NoData => write!(f, "The responder didn't provide any data"),
Self::Timeout => write!(f, "Operation timed out"),
Self::TooMuchData => write!(f, "Device replied with more data than claimed"),
}
}
}
impl core::error::Error for UsbError {}
impl From<nusb::Error> for UsbError {
fn from(error: nusb::Error) -> Self {
Self::Native(error)
}
}
impl From<nusb::transfer::TransferError> for UsbError {
fn from(error: nusb::transfer::TransferError) -> Self {
Self::Transfer(error)
}
}