Skip to main content

mailrs_mta_sts/
error.rs

1//! Error type for MTA-STS parsers and decisions.
2
3/// Errors returned by `mailrs-mta-sts` parsers.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum MtaStsError {
6    /// TXT record didn't start with `v=STSv1`.
7    NotAnStsRecord,
8    /// TXT record missing the required `id=` tag.
9    MissingId,
10    /// Policy file missing a required field (`version`, `mode`, `mx`, or `max_age`).
11    MissingField(&'static str),
12    /// `version` field was not `STSv1`.
13    UnsupportedVersion(String),
14    /// `mode` value was not one of `enforce|testing|none`.
15    InvalidMode(String),
16    /// `max_age` not a non-negative integer.
17    InvalidMaxAge(String),
18    /// `id` value too long (max 32 chars per RFC 8461 §3.1).
19    IdTooLong(usize),
20}
21
22impl std::fmt::Display for MtaStsError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            Self::NotAnStsRecord => write!(f, "not an MTA-STS record (missing v=STSv1)"),
26            Self::MissingId => write!(f, "MTA-STS record missing required id= tag"),
27            Self::MissingField(n) => write!(f, "MTA-STS policy missing required field: {n}"),
28            Self::UnsupportedVersion(v) => write!(f, "unsupported MTA-STS version: {v}"),
29            Self::InvalidMode(m) => write!(f, "invalid mode: {m}"),
30            Self::InvalidMaxAge(s) => write!(f, "invalid max_age: {s}"),
31            Self::IdTooLong(n) => write!(f, "id too long: {n} chars (max 32)"),
32        }
33    }
34}
35
36impl std::error::Error for MtaStsError {}