simple_dns/
simple_dns_error.rs

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