simple_dns/
simple_dns_error.rs

1use crate::lib::fmt::{Display, Formatter, Result};
2use crate::lib::{Error, FromUtf8Error, TryFromSliceError};
3
4/// Error types for SimpleDns
5#[derive(Debug, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum SimpleDnsError {
8    /// Invalid value for CLASS type
9    InvalidClass(u16),
10    /// Invalid value for QCLASS type
11    InvalidQClass(u16),
12    /// Invalid value for QTYPE type
13    InvalidQType(u16),
14    /// Service Name doesn't follow RFC rules
15    InvalidServiceName,
16    /// Service Name Label doesn't follow RFC rules
17    InvalidServiceLabel,
18    /// Character String doesn't follow RFC rules
19    InvalidCharacterString,
20    /// Provided data is not valid for a header
21    InvalidHeaderData,
22    /// Provided data is not valid for a DNS Packet
23    InvalidDnsPacket,
24    /// Attempted to perform an invalid operation
25    AttemptedInvalidOperation,
26    /// Incomplete dns packet, should try again after more data available
27    InsufficientData,
28    /// Failed to write the packet to the provided buffer
29    FailedToWrite,
30    /// Invalid utf8 string
31    InvalidUtf8String(FromUtf8Error),
32}
33
34impl From<TryFromSliceError> for SimpleDnsError {
35    fn from(_: TryFromSliceError) -> Self {
36        Self::InvalidDnsPacket
37    }
38}
39
40#[cfg(feature = "std")]
41impl From<std::io::Error> for SimpleDnsError {
42    fn from(_value: std::io::Error) -> Self {
43        Self::FailedToWrite
44    }
45}
46
47impl Error for SimpleDnsError {}
48
49impl Display for SimpleDnsError {
50    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
51        match self {
52            SimpleDnsError::InvalidClass(class) => {
53                write!(f, "Provided class is invalid: {class}")
54            }
55            SimpleDnsError::InvalidQClass(qclass) => {
56                write!(f, "Provided Qclass is invalid: {qclass}")
57            }
58            SimpleDnsError::InvalidQType(qtype) => {
59                write!(f, "Provided QType is invalid: {qtype}")
60            }
61            SimpleDnsError::InvalidServiceName => write!(f, "Provided service name is not valid"),
62            SimpleDnsError::InvalidServiceLabel => {
63                write!(f, "Provied service name contains invalid label")
64            }
65            SimpleDnsError::InvalidCharacterString => {
66                write!(f, "Provided character string is not valid")
67            }
68            SimpleDnsError::InvalidHeaderData => {
69                write!(f, "Provided header information is invalid")
70            }
71            SimpleDnsError::InvalidDnsPacket => {
72                write!(f, "Provided information is not a valid DNS packet")
73            }
74            SimpleDnsError::AttemptedInvalidOperation => {
75                write!(f, "Attempted to perform an invalid operation")
76            }
77            SimpleDnsError::InsufficientData => write!(f, "Incomplete dns packet"),
78            SimpleDnsError::FailedToWrite => {
79                write!(f, "Failed to write the packet to provided buffer")
80            }
81            SimpleDnsError::InvalidUtf8String(e) => {
82                write!(f, "Invalid utf8 string: {e}")
83            }
84        }
85    }
86}