simple_dns/
simple_dns_error.rs1use crate::lib::fmt::{Display, Formatter, Result};
2use crate::lib::{Error, FromUtf8Error, TryFromSliceError};
3
4#[derive(Debug, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum SimpleDnsError {
8 InvalidClass(u16),
10 InvalidQClass(u16),
12 InvalidQType(u16),
14 InvalidServiceName,
16 InvalidServiceLabel,
18 InvalidCharacterString,
20 InvalidHeaderData,
22 InvalidDnsPacket,
24 AttemptedInvalidOperation,
26 InsufficientData,
28 FailedToWrite,
30 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}