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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;
use std::net::TcpStream;
use std::result;
use std::str::Utf8Error;
use base64::DecodeError;
use bufstream::IntoInnerError as BufError;
use imap_proto::{types::ResponseCode, Response};
#[cfg(feature = "native-tls")]
use native_tls::Error as TlsError;
#[cfg(feature = "native-tls")]
use native_tls::HandshakeError as TlsHandshakeError;
#[cfg(feature = "rustls-tls")]
use rustls_connector::HandshakeError as RustlsHandshakeError;
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub struct Bad {
pub information: String,
pub code: Option<ResponseCode<'static>>,
}
impl fmt::Display for Bad {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.information)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct No {
pub information: String,
pub code: Option<ResponseCode<'static>>,
}
impl fmt::Display for No {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.information)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Bye {
pub information: String,
pub code: Option<ResponseCode<'static>>,
}
impl fmt::Display for Bye {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.information)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(IoError),
#[cfg(feature = "rustls-tls")]
RustlsHandshake(RustlsHandshakeError<TcpStream>),
#[cfg(feature = "native-tls")]
TlsHandshake(TlsHandshakeError<TcpStream>),
#[cfg(feature = "native-tls")]
Tls(TlsError),
Bad(Bad),
No(No),
Bye(Bye),
ConnectionLost,
Parse(ParseError),
Validate(ValidateError),
Append,
Unexpected(Response<'static>),
MissingStatusResponse,
}
impl From<IoError> for Error {
fn from(err: IoError) -> Error {
Error::Io(err)
}
}
impl From<ParseError> for Error {
fn from(err: ParseError) -> Error {
Error::Parse(err)
}
}
impl<T> From<BufError<T>> for Error {
fn from(err: BufError<T>) -> Error {
Error::Io(err.into())
}
}
#[cfg(feature = "rustls-tls")]
impl From<RustlsHandshakeError<TcpStream>> for Error {
fn from(err: RustlsHandshakeError<TcpStream>) -> Error {
Error::RustlsHandshake(err)
}
}
#[cfg(feature = "native-tls")]
impl From<TlsHandshakeError<TcpStream>> for Error {
fn from(err: TlsHandshakeError<TcpStream>) -> Error {
Error::TlsHandshake(err)
}
}
#[cfg(feature = "native-tls")]
impl From<TlsError> for Error {
fn from(err: TlsError) -> Error {
Error::Tls(err)
}
}
impl<'a> From<Response<'a>> for Error {
fn from(err: Response<'a>) -> Error {
Error::Unexpected(err.into_owned())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::Io(ref e) => fmt::Display::fmt(e, f),
#[cfg(feature = "rustls-tls")]
Error::RustlsHandshake(ref e) => fmt::Display::fmt(e, f),
#[cfg(feature = "native-tls")]
Error::Tls(ref e) => fmt::Display::fmt(e, f),
#[cfg(feature = "native-tls")]
Error::TlsHandshake(ref e) => fmt::Display::fmt(e, f),
Error::Validate(ref e) => fmt::Display::fmt(e, f),
Error::Parse(ref e) => fmt::Display::fmt(e, f),
Error::No(ref data) => write!(f, "No Response: {}", data),
Error::Bad(ref data) => write!(f, "Bad Response: {}", data),
Error::Bye(ref data) => write!(f, "Bye Response: {}", data),
Error::ConnectionLost => f.write_str("Connection Lost"),
Error::Append => f.write_str("Could not append mail to mailbox"),
Error::Unexpected(ref r) => write!(f, "Unexpected Response: {:?}", r),
Error::MissingStatusResponse => write!(f, "Missing STATUS Response"),
}
}
}
impl StdError for Error {
#[allow(deprecated)]
fn description(&self) -> &str {
match *self {
Error::Io(ref e) => e.description(),
#[cfg(feature = "rustls-tls")]
Error::RustlsHandshake(ref e) => e.description(),
#[cfg(feature = "native-tls")]
Error::Tls(ref e) => e.description(),
#[cfg(feature = "native-tls")]
Error::TlsHandshake(ref e) => e.description(),
Error::Parse(ref e) => e.description(),
Error::Validate(ref e) => e.description(),
Error::Bad(_) => "Bad Response",
Error::No(_) => "No Response",
Error::Bye(_) => "Bye Response",
Error::ConnectionLost => "Connection lost",
Error::Append => "Could not append mail to mailbox",
Error::Unexpected(_) => "Unexpected Response",
Error::MissingStatusResponse => "Missing STATUS Response",
}
}
fn cause(&self) -> Option<&dyn StdError> {
match *self {
Error::Io(ref e) => Some(e),
#[cfg(feature = "rustls-tls")]
Error::RustlsHandshake(ref e) => Some(e),
#[cfg(feature = "native-tls")]
Error::Tls(ref e) => Some(e),
#[cfg(feature = "native-tls")]
Error::TlsHandshake(ref e) => Some(e),
Error::Parse(ParseError::DataNotUtf8(_, ref e)) => Some(e),
_ => None,
}
}
}
#[derive(Debug)]
pub enum ParseError {
Invalid(Vec<u8>),
Authentication(String, Option<DecodeError>),
DataNotUtf8(Vec<u8>, Utf8Error),
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ParseError::Invalid(_) => f.write_str("Unable to parse status response"),
ParseError::Authentication(_, _) => {
f.write_str("Unable to parse authentication response")
}
ParseError::DataNotUtf8(_, _) => f.write_str("Unable to parse data as UTF-8 text"),
}
}
}
impl StdError for ParseError {
fn description(&self) -> &str {
match *self {
ParseError::Invalid(_) => "Unable to parse status response",
ParseError::Authentication(_, _) => "Unable to parse authentication response",
ParseError::DataNotUtf8(_, _) => "Unable to parse data as UTF-8 text",
}
}
fn cause(&self) -> Option<&dyn StdError> {
match *self {
ParseError::Authentication(_, Some(ref e)) => Some(e),
_ => None,
}
}
}
#[derive(Debug)]
pub struct ValidateError {
pub(crate) command_synopsis: String,
pub(crate) argument: String,
pub(crate) offending_char: char,
}
impl fmt::Display for ValidateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Invalid character {:?} in argument '{}' of command '{}'",
self.offending_char, self.argument, self.command_synopsis
)
}
}
impl StdError for ValidateError {
fn description(&self) -> &str {
"Invalid character in command argument"
}
fn cause(&self) -> Option<&dyn StdError> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validate_error_display() {
assert_eq!(
ValidateError {
command_synopsis: "COMMAND arg1 arg2".to_owned(),
argument: "arg2".to_string(),
offending_char: '\n'
}
.to_string(),
"Invalid character '\\n' in argument 'arg2' of command 'COMMAND arg1 arg2'"
);
}
}