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
53
54
55
56
57
58
59
60
61
use super::StandardResponseCode;
use std::{fmt, io};
#[derive(Debug)]
pub enum Error {
Response(u16),
Malformed(String),
Usb(rusb::Error),
Io(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Response(r) => write!(
f,
"{} (0x{:04x})",
StandardResponseCode::name(r).unwrap_or("Unknown"),
r
),
Error::Usb(ref e) => write!(f, "USB error: {}", e),
Error::Io(ref e) => write!(f, "IO error: {}", e),
Error::Malformed(ref e) => write!(f, "{}", e),
}
}
}
impl ::std::error::Error for Error {
fn cause(&self) -> Option<&dyn (::std::error::Error)> {
match *self {
Error::Usb(ref e) => Some(e),
Error::Io(ref e) => Some(e),
_ => None,
}
}
}
impl From<rusb::Error> for Error {
fn from(e: rusb::Error) -> Error {
Error::Usb(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
match e.kind() {
io::ErrorKind::UnexpectedEof => {
Error::Malformed("Unexpected end of message".to_string())
}
_ => Error::Io(e),
}
}
}