1use std::{io, fmt, error};
2
3#[derive(Debug, PartialEq, Clone)]
4pub enum HWKeyError {
5 Unavailable,
7
8 Unsupported(u16),
10
11 DeviceLocked,
12
13 EmptyResponse,
15 CryptoError(String),
17 CommError(String),
19 EncodingError(String),
21 OtherError(String),
23 InputError(String),
25
26 InvalidResponse(ResponseError)
27}
28
29#[derive(Debug, PartialEq, Clone, Copy)]
30pub enum ResponseError {
31 ShortLedgerVersion,
32}
33
34impl From<io::Error> for HWKeyError {
35 fn from(err: io::Error) -> Self {
36 HWKeyError::CommError(err.to_string())
37 }
38}
39
40impl From<&str> for HWKeyError {
41 fn from(err: &str) -> Self {
42 HWKeyError::OtherError(err.to_string())
43 }
44}
45
46impl From<hidapi::HidError> for HWKeyError {
47 fn from(err: hidapi::HidError) -> Self {
48 HWKeyError::CommError(err.to_string())
49 }
50}
51
52impl fmt::Display for HWKeyError {
53 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54 match *self {
55 HWKeyError::Unavailable => write!(f, "HWKey Unavailable"),
56 HWKeyError::Unsupported(pid) => write!(f, "HWKey Unsupported device with PID: 0x{:04x}", pid),
57 HWKeyError::EmptyResponse => write!(f, "HWKey no answer"),
58 HWKeyError::CryptoError(ref str) => write!(f, "HWKey error: {}", str),
59 HWKeyError::CommError(ref str) => write!(f, "Communication protocol error: {}", str),
60 HWKeyError::EncodingError(ref str) => write!(f, "Encoding error: {}", str),
61 HWKeyError::OtherError(ref str) => write!(f, "HWKey other error: {}", str),
62 HWKeyError::InputError(ref str) => write!(f, "HWKey invalid input: {}", str),
63 HWKeyError::InvalidResponse(e) => write!(f, "HWKey invalid resp: {:?}", e),
64 HWKeyError::DeviceLocked => write!(f, "HWKey Locked"),
65 }
66 }
67}
68
69impl error::Error for HWKeyError {
70 fn description(&self) -> &str {
71 "HWKey error"
72 }
73
74 fn cause(&self) -> Option<&dyn error::Error> {
75 match *self {
76 HWKeyError::InvalidResponse(ref e) => Some(e),
77 _ => None,
78 }
79 }
80}
81
82impl fmt::Display for ResponseError {
83 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84 match *self {
85 ResponseError::ShortLedgerVersion => write!(f, "Short Ledger version response"),
86 }
87 }
88}
89
90impl error::Error for ResponseError {
91 fn description(&self) -> &str {
92 "Response error"
93 }
94
95 fn cause(&self) -> Option<&dyn error::Error> {
96 None
97 }
98}