daaki-message 0.2.0

RFC 5322 email message parser and builder
Documentation
//! Typed error enum for email message parsing and building.
//!
//! # References
//! - RFC 5322 (Internet Message Format)
//! - RFC 2045–2047 (MIME)

/// Errors that can occur during email message parsing or building.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Error {
    /// Empty input provided to the parser (RFC 5322 Section 3.5 — a valid
    /// message must contain at least header fields, so zero bytes is invalid).
    #[error("empty input: no message bytes provided")]
    EmptyInput,

    /// Missing required `From` header (RFC 5322 Section 3.6.2).
    #[error("missing required From header (RFC 5322 Section 3.6.2)")]
    MissingFrom,

    /// Date header could not be parsed per RFC 5322 Section 3.3.
    #[error("invalid date: {0}")]
    InvalidDate(String),

    /// Syntactically invalid email address (RFC 5322 Section 3.4).
    #[error("invalid email address: {0}")]
    InvalidAddress(String),

    /// Missing `Sender` header when `From` has multiple addresses
    /// (RFC 5322 Section 3.6.2).
    ///
    /// RFC 5322: "If the from field contains more than one mailbox
    /// specification in the mailbox-list, then the sender field, containing
    /// the field value corresponding to the responsible agent of the message
    /// MUST be present in the message."
    #[error(
        "Sender header required when From has multiple addresses \
         (RFC 5322 Section 3.6.2)"
    )]
    MissingSender,

    /// Invalid header field name (RFC 5322 Section 2.2).
    ///
    /// The name is empty, contains non-ftext characters, or is otherwise
    /// malformed per RFC 5322 Section 2.2 (`field-name = 1*ftext`).
    #[error("invalid header name: {0}")]
    InvalidHeaderName(String),

    /// Invalid message-ID (RFC 5322 Section 3.6.4).
    ///
    /// The value is empty, missing the `@` separator, contains prohibited
    /// characters, or has empty `id-left` / `id-right` parts.
    #[error("invalid message-ID: {0}")]
    InvalidMessageId(String),

    /// A header value contains a token or whitespace run that cannot be folded
    /// within the 998-octet line limit (RFC 5322 Sections 2.1.1, 2.2.3).
    #[error("header line too long: {0}")]
    HeaderLineTooLong(String),

    /// An `extra_headers` entry uses a field name reserved for standard headers
    /// managed by the builder (RFC 5322 Section 3.6).
    #[error("reserved header name: {0}")]
    ReservedHeaderName(String),

    /// An attachment violates MIME constraints — invalid content encoding,
    /// missing required headers, or prohibited bytes (RFC 2045/2046).
    #[error("invalid attachment: {0}")]
    InvalidAttachment(String),

    /// A trace header (Return-Path / Received) violates
    /// RFC 5322 Section 3.6.7 syntax or cardinality rules.
    #[error("invalid trace header: {0}")]
    InvalidTraceHeader(String),

    /// A Resent-* header violates RFC 5322 Section 3.6.6 syntax,
    /// cardinality, or block-grouping rules.
    #[error("invalid resent header: {0}")]
    InvalidResentHeader(String),
}

#[cfg(test)]
#[path = "error_tests.rs"]
mod tests;