simple_dns/
simple_dns_error.rs1use std::{array::TryFromSliceError, error::Error, fmt::Display};
2
3#[derive(Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum SimpleDnsError {
7 InvalidClass(u16),
9 InvalidQClass(u16),
11 InvalidQType(u16),
13 InvalidServiceName,
15 InvalidServiceLabel,
17 InvalidCharacterString,
19 InvalidHeaderData,
21 InvalidDnsPacket,
23 AttemptedInvalidOperation,
25 InsufficientData,
27 FailedToWrite,
29 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}