Skip to main content

imap_core/
error.rs

1use thiserror::Error;
2
3/// Errors produced by the IMAP response parser.
4#[derive(Error, Debug, PartialEq, Eq)]
5pub enum ParseError {
6    /// Input ended before a complete response could be parsed.
7    /// The caller should accumulate more bytes and retry.
8    #[error("Incomplete input")]
9    Incomplete,
10    /// A byte that is invalid at the current grammar position was found.
11    /// The value is the byte offset into the input where it occurred.
12    #[error("Invalid character at offset {0}")]
13    InvalidChar(usize),
14    /// An expected literal token was not present at the given offset.
15    #[error("Expected literal {expected:?} at offset {offset}")]
16    ExpectedLiteral {
17        /// The literal token the parser expected to find.
18        expected: String,
19        /// Byte offset into the input where the literal was expected.
20        offset: usize,
21    },
22    /// Input ended unexpectedly in the middle of a token.
23    #[error("Unexpected end of input")]
24    UnexpectedEof,
25    /// A numeric field could not be parsed as a valid integer.
26    #[error("Number parsing error")]
27    InvalidNumber,
28    /// A literal whose declared length exceeds the parser's hard cap.
29    /// Used as an explicit DoS guard against hostile servers.
30    #[error("Literal length {got} exceeds maximum {max}")]
31    LiteralTooLarge {
32        /// Literal length declared by the server, in octets.
33        got: usize,
34        /// Maximum literal length the parser accepts, in octets.
35        max: usize,
36    },
37    /// Well-formed bytes that nonetheless violate the response grammar.
38    /// The value is a human-readable description of the violation.
39    #[error("Malformed response: {0}")]
40    Malformed(&'static str),
41    /// A parsing error that does not fit any of the more specific variants.
42    #[error("Other parsing error: {0}")]
43    Other(&'static str),
44}