1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::error;
use std::fmt;
use rusb::Error as usbError;
use std::io::Error as ioError;

#[derive(Debug)]
pub enum YubicoError {
    IOError(ioError),
    UsbError(usbError),
    CommandNotSupported,
    DeviceNotFound,
    OpenDeviceError,
    CanNotWriteToDevice,
    WrongCRC,
    ConfigNotWritten,
}

impl fmt::Display for YubicoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            YubicoError::IOError(ref err) => write!(f, "IO error: {}", err),
            YubicoError::UsbError(ref err) => write!(f, "USB  error: {}", err),
            YubicoError::DeviceNotFound => write!(f, "Device not found"),
            YubicoError::OpenDeviceError => write!(f, "Can not open device"),
            YubicoError::CommandNotSupported => write!(f, "Command Not Supported"),
            YubicoError::WrongCRC => write!(f, "Wrong CRC"),            
            YubicoError::CanNotWriteToDevice => write!(f, "Can not write to Device"),
            YubicoError::ConfigNotWritten => write!(f, "Configuration has failed"),
        }
    }
}

impl error::Error for YubicoError {
    fn cause(&self) -> Option<& dyn error::Error> {
        match *self {
            YubicoError::UsbError(ref err) => Some(err),                    
            _ => None
        }
    }
}

impl From<ioError> for YubicoError {
    fn from(err: ioError) -> YubicoError {
        YubicoError::IOError(err)
    }
}

impl From<usbError> for YubicoError {
    fn from(err: usbError) -> YubicoError {
        YubicoError::UsbError(err)
    }
}