use std::error;
use std::fmt;
use hid;
use libusb;
#[derive(Debug)]
pub enum Error {
Hid(hid::Error),
Usb(libusb::Error),
DeviceNotFound,
DeviceDisconnected,
UnknownHidVersion,
UnexpectedChunkSizeFromDevice(usize),
DeviceReadTimeout,
DeviceBadMagic,
DeviceBadSessionId,
DeviceUnexpectedSequenceNumber,
InvalidMessageType(u32),
NoDeviceSerial,
}
impl From<hid::Error> for Error {
fn from(e: hid::Error) -> Error {
Error::Hid(e)
}
}
impl From<libusb::Error> for Error {
fn from(e: libusb::Error) -> Error {
Error::Usb(e)
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Hid(ref e) => Some(e),
Error::Usb(ref e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Hid(ref e) => fmt::Display::fmt(e, f),
Error::Usb(ref e) => fmt::Display::fmt(e, f),
Error::DeviceNotFound => write!(f, "the device to connect to was not found"),
Error::DeviceDisconnected => write!(f, "the device is no longer available"),
Error::UnknownHidVersion => write!(f, "HID version of the device unknown"),
Error::DeviceReadTimeout => write!(f, "timeout expired while reading from device"),
Error::DeviceBadMagic => write!(f, "the device sent chunk with wrong magic value"),
Error::DeviceBadSessionId => {
write!(f, "the device sent a message with a wrong session id")
}
Error::DeviceUnexpectedSequenceNumber => {
write!(f, "the device sent an unexpected sequence number")
}
Error::UnexpectedChunkSizeFromDevice(s) => write!(f, "{}: {}", self, s),
Error::InvalidMessageType(ref t) => write!(f, "{}: {}", self, t),
Error::NoDeviceSerial => write!(f, "unable to determine device serial number"),
}
}
}