mime_tree/error.rs
1use std::fmt;
2
3/// Error returned when `parse()` cannot produce any result from the input bytes.
4///
5/// Best-effort parsing: malformed-but-parseable input yields a `ParsedMessage`
6/// with `warnings` populated. Only truly unrecoverable input returns `Err`.
7#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
8#[non_exhaustive]
9pub enum ParseError {
10 /// The input byte slice is empty.
11 EmptyInput,
12 /// The input contains no recognizable RFC 5322 headers.
13 NoHeaders,
14 /// The byte range specified in a `ParsedPart` extends beyond the raw message bytes.
15 ///
16 /// `offset` and `length` are `u32` to match `ParsedPart::body_range`.
17 /// `available` is `u64` because it comes from `raw.len() as u64` —
18 /// using `u64` avoids a lossy truncation on platforms where `usize > u32`
19 /// and makes the error message unambiguous even if the slice length
20 /// exceeds 4 GiB (which `ParsedPart` cannot address, but the caller's
21 /// buffer might be that large).
22 InvalidRange {
23 offset: u32,
24 length: u32,
25 available: u64,
26 },
27}
28
29impl fmt::Display for ParseError {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 ParseError::EmptyInput => write!(f, "input is empty"),
33 ParseError::NoHeaders => write!(f, "input contains no RFC 5322 headers"),
34 ParseError::InvalidRange {
35 offset,
36 length,
37 available,
38 } => write!(
39 f,
40 "body range [{}..{}] extends beyond message length {}",
41 offset,
42 u64::from(*offset) + u64::from(*length),
43 available,
44 ),
45 }
46 }
47}
48
49impl std::error::Error for ParseError {}