simple_dns/
simple_dns_error.rsuse std::{array::TryFromSliceError, error::Error, fmt::Display};
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SimpleDnsError {
InvalidClass(u16),
InvalidQClass(u16),
InvalidQType(u16),
InvalidServiceName,
InvalidServiceLabel,
InvalidCharacterString,
InvalidHeaderData,
InvalidDnsPacket,
AttemptedInvalidOperation,
InsufficientData,
FailedToWrite,
InvalidUtf8String(std::string::FromUtf8Error),
}
impl From<TryFromSliceError> for SimpleDnsError {
fn from(_: TryFromSliceError) -> Self {
Self::InvalidDnsPacket
}
}
impl From<std::io::Error> for SimpleDnsError {
fn from(_value: std::io::Error) -> Self {
Self::FailedToWrite
}
}
impl Error for SimpleDnsError {}
impl Display for SimpleDnsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SimpleDnsError::InvalidClass(class) => {
write!(f, "Provided class is invalid: {0}", class)
}
SimpleDnsError::InvalidQClass(qclass) => {
write!(f, "Provided Qclass is invalid: {0}", qclass)
}
SimpleDnsError::InvalidQType(qtype) => {
write!(f, "Provided QType is invalid: {0}", qtype)
}
SimpleDnsError::InvalidServiceName => write!(f, "Provided service name is not valid"),
SimpleDnsError::InvalidServiceLabel => {
write!(f, "Provied service name contains invalid label")
}
SimpleDnsError::InvalidCharacterString => {
write!(f, "Provided character string is not valid")
}
SimpleDnsError::InvalidHeaderData => {
write!(f, "Provided header information is invalid")
}
SimpleDnsError::InvalidDnsPacket => {
write!(f, "Provided information is not a valid DNS packet")
}
SimpleDnsError::AttemptedInvalidOperation => {
write!(f, "Attempted to perform an invalid operation")
}
SimpleDnsError::InsufficientData => write!(f, "Incomplete dns packet"),
SimpleDnsError::FailedToWrite => {
write!(f, "Failed to write the packet to provided buffer")
}
SimpleDnsError::InvalidUtf8String(e) => {
write!(f, "Invalid utf8 string: {}", e)
}
}
}
}