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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::fmt;

/// Convenience alias for the library [`Result`](std::result::Result).
pub type Result<T> = std::result::Result<T, Error>;

/// Represents error variants for the library.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
    InvalidBillAcceptorState(u8),
    InvalidDenomination((u8, u8)),
    InvalidFailureCode(u8),
    InvalidStatusCode(u8),
    InvalidMessageId(u8),
    InvalidConfId(u8),
    InvalidFuncId(u8),
    InvalidMessageType(u8),
    InvalidMessageLen((usize, usize)),
    InvalidMessageDataLen((usize, usize)),
    InvalidFunctionMode(u8),
    InvalidMajorMinorStatus(u16),
    InvalidDeviceStatusLen((usize, usize)),
    InvalidDeviceStatus(u16),
    InvalidRequestCode(u16),
    InvalidEventCode(u16),
    InvalidMessageCode((u8, u16)),
    InvalidResponseCode(u8),
    InvalidResponseLen((usize, usize)),
    InvalidUnitNumber(u8),
    InvalidFunctionStatus(u8),
    InvalidUnitStatusLen((usize, usize)),
    InvalidUnitStatusListLen((usize, usize)),
    InvalidStackRequestLen((usize, usize)),
    InvalidEventLen((usize, usize)),
    InvalidRequestLen((usize, usize)),
    InvalidStackStatusChange(u8),
    InvalidRejectCode(u8),
    InvalidRequestType(u8),
    InvalidEventType(u8),
    InvalidMessage(((u8, u16), (u8, u16))),
    InvalidEscrowData,
    InvalidCurrency((u32, u16)),
    InvalidEscrowDataLen((usize, usize)),
    InvalidCurrencyLen((usize, usize)),
    InvalidDenominationLen((usize, usize)),
    InvalidTicketLen((usize, usize)),
    InvalidAsciiString,
    InvalidUtf8String,
    #[cfg(feature = "usb")]
    Usb(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidBillAcceptorState(err) => write!(f, "invalid bill acceptor state: {err}"),
            Self::InvalidDenomination((int, exp)) => write!(
                f,
                r#"invalid denomination: {{"integer": {int}, "exponent": {exp}}}"#
            ),
            Self::InvalidFailureCode(err) => write!(f, "invalid failure code: {err}"),
            Self::InvalidStatusCode(err) => write!(f, "invalid status code: {err}"),
            Self::InvalidMessageId(err) => write!(f, "invalid message ID: {err}"),
            Self::InvalidConfId(err) => write!(f, "invalid conf ID: {err}"),
            Self::InvalidFuncId(err) => write!(f, "invalid func ID: {err}"),
            Self::InvalidMessageType(err) => write!(f, "invalid message type: {err}"),
            Self::InvalidMessageLen((have, exp)) => {
                write!(f, "invalid message length, have: {have}, expected: {exp}")
            }
            Self::InvalidMessageDataLen((have, exp)) => write!(
                f,
                "invalid message data length, have: {have}, expected: {exp}"
            ),
            Self::InvalidFunctionMode(err) => {
                write!(f, "invalid device status function mode: {err}")
            }
            Self::InvalidMajorMinorStatus(err) => {
                write!(f, "invalid device status major-minor status: {err}")
            }
            Self::InvalidDeviceStatus(err) => {
                write!(f, "invalid device status: {err}")
            }
            Self::InvalidDeviceStatusLen((have, exp)) => {
                write!(
                    f,
                    "invalid device status length, have: {have}, expected: {exp}"
                )
            }
            Self::InvalidRequestCode(err) => write!(f, "invalid request code: {err}"),
            Self::InvalidEventCode(err) => write!(f, "invalid event code: {err}"),
            Self::InvalidMessageCode((ty, code)) => write!(
                f,
                r#"invalid message code: {{"message_type": {ty}, "code": {code}}}"#
            ),
            Self::InvalidResponseCode(err) => write!(f, "invalid response code: {err}"),
            Self::InvalidResponseLen((have, exp)) => {
                write!(f, "invalid response length, have: {have}, expected: {exp}")
            }
            Self::InvalidUnitNumber(err) => write!(f, "invalid unit number: {err:#x}"),
            Self::InvalidFunctionStatus(err) => {
                write!(f, "invalid function status: {err:#x}")
            }
            Self::InvalidUnitStatusLen((have, exp)) => {
                write!(
                    f,
                    "invalid unit status length, have: {have}, expected: {exp}"
                )
            }
            Self::InvalidUnitStatusListLen((have, exp)) => {
                write!(
                    f,
                    "invalid unit status list length, have: {have}, expected a multiple of: {exp}"
                )
            }
            Self::InvalidStackRequestLen((have, exp)) => {
                write!(
                    f,
                    "invalid stack request length, have: {have}, expected: {exp}"
                )
            }
            Self::InvalidEventLen((have, exp)) => {
                write!(f, "invalid event length, have: {have}, expected: {exp}")
            }
            Self::InvalidRequestLen((have, exp)) => {
                write!(f, "invalid request length, have: {have}, expected: {exp}")
            }
            Self::InvalidStackStatusChange(err) => {
                write!(f, "invalid stack status change: {err:#x}")
            }
            Self::InvalidRejectCode(err) => {
                write!(f, "invalid reject code: {err:#x}")
            }
            Self::InvalidRequestType(err) => {
                write!(f, "invalid request type: {err:#x}")
            }
            Self::InvalidEventType(err) => {
                write!(f, "invalid event type: {err:#x}")
            }
            Self::InvalidMessage(((have_type, have_code), (exp_type, exp_code))) => {
                write!(f, "invalid message, have: {have_type} - {have_code}, expected: {exp_type} - {exp_code}")
            }
            Self::InvalidEscrowData => write!(f, "invalid escrow data"),
            Self::InvalidCurrency((code, denom)) => {
                write!(
                    f,
                    r#"invalid currency: {{"code": {code}, "denomination": {denom}}}"#
                )
            }
            Self::InvalidEscrowDataLen((have, exp)) => {
                write!(
                    f,
                    "invalid escrow data length, have: {have}, expected: {exp}"
                )
            }
            Self::InvalidCurrencyLen((have, exp)) => {
                write!(f, "invalid currency length, have: {have}, expected: {exp}")
            }
            Self::InvalidDenominationLen((have, exp)) => {
                write!(
                    f,
                    "invalid denomination length, have: {have}, expected: {exp}"
                )
            }
            Self::InvalidTicketLen((have, exp)) => {
                write!(f, "invalid ticket length, have: {have}, expected: {exp}")
            }
            Self::InvalidAsciiString => write!(f, "invalid ASCII encoded string"),
            Self::InvalidUtf8String => write!(f, "invalid UTF-8 encoded string"),
            #[cfg(feature = "usb")]
            Self::Usb(err) => write!(f, "USB error: {err}"),
        }
    }
}

impl std::error::Error for Error {}