async_imap/
error.rs

1//! IMAP error types.
2
3use std::io::Error as IoError;
4use std::str::Utf8Error;
5
6use base64::DecodeError;
7
8/// A convenience wrapper around `Result` for `imap::Error`.
9pub type Result<T> = std::result::Result<T, Error>;
10
11/// A set of errors that can occur in the IMAP client
12#[derive(thiserror::Error, Debug)]
13#[non_exhaustive]
14pub enum Error {
15    /// An `io::Error` that occurred while trying to read or write to a network stream.
16    #[error("io: {0}")]
17    Io(#[from] IoError),
18    /// A BAD response from the IMAP server.
19    #[error("bad response: {0}")]
20    Bad(String),
21    /// A NO response from the IMAP server.
22    #[error("no response: {0}")]
23    No(String),
24    /// The connection was terminated unexpectedly.
25    #[error("connection lost")]
26    ConnectionLost,
27    /// Error parsing a server response.
28    #[error("parse: {0}")]
29    Parse(#[from] ParseError),
30    /// Command inputs were not valid [IMAP
31    /// strings](https://tools.ietf.org/html/rfc3501#section-4.3).
32    #[error("validate: {0}")]
33    Validate(#[from] ValidateError),
34    /// Error appending an e-mail.
35    #[error("could not append mail to mailbox")]
36    Append,
37}
38
39/// An error occured while trying to parse a server response.
40#[derive(thiserror::Error, Debug)]
41pub enum ParseError {
42    /// Indicates an error parsing the status response. Such as OK, NO, and BAD.
43    #[error("unable to parse status response")]
44    Invalid(Vec<u8>),
45    /// An unexpected response was encountered.
46    #[error("encountered unexpected parsed response: {0}")]
47    Unexpected(String),
48    /// The client could not find or decode the server's authentication challenge.
49    #[error("unable to parse authentication response: {0} - {1:?}")]
50    Authentication(String, Option<DecodeError>),
51    /// The client received data that was not UTF-8 encoded.
52    #[error("unable to parse data ({0:?}) as UTF-8 text: {1:?}")]
53    DataNotUtf8(Vec<u8>, #[source] Utf8Error),
54    /// The expected response for X was not found
55    #[error("expected response not found for: {0}")]
56    ExpectedResponseNotFound(String),
57}
58
59/// An [invalid character](https://tools.ietf.org/html/rfc3501#section-4.3) was found in an input
60/// string.
61#[derive(thiserror::Error, Debug)]
62#[error("invalid character in input: '{0}'")]
63pub struct ValidateError(pub char);
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    fn is_send<T: Send>(_t: T) {}
70
71    #[test]
72    fn test_send() {
73        is_send::<Result<usize>>(Ok(3));
74    }
75}