1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::error;
use std::io;
use std::fmt;
use std::result;
use ::bits::{ComposeError, ParseError};
use ::iana::Rcode;
#[derive(Debug)]
pub enum Error {
Question(ComposeError),
Timeout,
NoName,
NoSecureAnswers,
AllBogusAnswers,
Io(io::Error),
}
impl Error {
#[allow(match_same_arms)]
pub fn merge(self, other: Self) -> Self {
use self::Error::*;
match (self, other) {
(Question(err), _) => Question(err),
(Timeout, Io(_)) => Timeout,
(Timeout, other) => other,
(NoName, NoSecureAnswers) => NoSecureAnswers,
(NoName, AllBogusAnswers) => AllBogusAnswers,
(NoName, _) => NoName,
(NoSecureAnswers, _) => NoSecureAnswers,
(AllBogusAnswers, NoSecureAnswers) => NoSecureAnswers,
(AllBogusAnswers, _) => AllBogusAnswers,
(Io(_), other) => other
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
use self::Error::*;
match *self {
Question(ref error) => error.description(),
NoName => "all responses were negative",
Timeout => "all queries timed out",
NoSecureAnswers => "no received response was secure",
AllBogusAnswers => "all received responses were bogus",
Io(ref error) => error.description()
}
}
}
impl From<Rcode> for Error {
fn from(rcode: Rcode) -> Error {
match rcode {
Rcode::NXDomain => Error::NoName,
_ => Error::Timeout,
}
}
}
impl From<ComposeError> for Error {
fn from(error: ComposeError) -> Error {
Error::Question(error)
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error::Io(error)
}
}
impl From<ParseError> for Error {
fn from(error: ParseError) -> Error {
Error::Io(io::Error::from(error))
}
}
impl From<Error> for io::Error {
fn from(error: Error) -> io::Error {
io::Error::new(io::ErrorKind::Other, error)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::error::Error;
self.description().fmt(f)
}
}
pub type Result<T> = result::Result<T, Error>;