use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HostnameError {
Empty,
TooLong,
EmptyLabel,
LabelTooLong,
InvalidChar {
index: usize,
found: char,
},
HyphenEdge,
NumericLastLabel,
}
impl fmt::Display for HostnameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => write!(f, "hostname is empty"),
Self::TooLong => write!(f, "hostname is longer than 253 octets"),
Self::EmptyLabel => write!(f, "hostname has an empty label"),
Self::LabelTooLong => write!(f, "hostname has a label longer than 63 octets"),
Self::InvalidChar { index, found } => {
write!(f, "invalid hostname character {found:?} at byte {index}")
}
Self::HyphenEdge => write!(f, "hostname label starts or ends with a hyphen"),
Self::NumericLastLabel => {
write!(
f,
"hostname ends in a number, which URL parsers read as an IPv4 address"
)
}
}
}
}
impl std::error::Error for HostnameError {}
#[cfg(test)]
#[path = "error.test.rs"]
mod tests;