use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
position: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorKind {
Empty,
MissingAtSign,
EmptyLocalPart,
EmptyDomain,
LocalPartTooLong { len: usize },
AddressTooLong { len: usize },
DomainLabelTooLong { label: String, len: usize },
InvalidLocalPartChar { ch: char },
InvalidDomainChar { ch: char },
DomainLabelHyphen,
DomainNoDot,
UnterminatedQuotedString,
InvalidQuotedPair,
UnterminatedComment,
UnterminatedDomainLiteral,
IdnaError(String),
UnknownTld(String),
Unexpected { ch: char },
}
impl Error {
pub(crate) fn new(kind: ErrorKind, position: usize) -> Self {
Self { kind, position }
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
pub fn position(&self) -> usize {
self.position
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.kind {
ErrorKind::Empty => write!(f, "empty input"),
ErrorKind::MissingAtSign => write!(f, "missing '@' separator"),
ErrorKind::EmptyLocalPart => write!(f, "empty local part"),
ErrorKind::EmptyDomain => write!(f, "empty domain"),
ErrorKind::LocalPartTooLong { len } => {
write!(f, "local part too long: {len} octets (max 64)")
}
ErrorKind::AddressTooLong { len } => {
write!(f, "address too long: {len} octets (max 254)")
}
ErrorKind::DomainLabelTooLong { label, len } => {
write!(f, "domain label '{label}' too long: {len} octets (max 63)")
}
ErrorKind::InvalidLocalPartChar { ch } => {
write!(f, "invalid character in local part: '{ch}'")
}
ErrorKind::InvalidDomainChar { ch } => {
write!(f, "invalid character in domain: '{ch}'")
}
ErrorKind::DomainLabelHyphen => {
write!(f, "domain label starts or ends with hyphen")
}
ErrorKind::DomainNoDot => write!(f, "domain has no dot"),
ErrorKind::UnterminatedQuotedString => write!(f, "unterminated quoted string"),
ErrorKind::InvalidQuotedPair => write!(f, "invalid quoted-pair escape"),
ErrorKind::UnterminatedComment => write!(f, "unterminated comment"),
ErrorKind::UnterminatedDomainLiteral => write!(f, "unterminated domain literal"),
ErrorKind::IdnaError(msg) => write!(f, "IDNA encoding failed: {msg}"),
ErrorKind::UnknownTld(tld) => write!(f, "unknown TLD: .{tld}"),
ErrorKind::Unexpected { ch } => {
write!(
f,
"unexpected character '{ch}' at position {}",
self.position
)
}
}
}
}
impl std::error::Error for Error {}