1use {Error};
2
3#[derive(Debug, PartialEq, Eq, Clone, Copy)]
7pub enum Type {
8 A = 1,
10 NS = 2,
12 MF = 4,
14 CNAME = 5,
16 SOA = 6,
18 MB = 7,
20 MG = 8,
22 MR = 9,
24 NULL = 10,
26 WKS = 11,
28 PTR = 12,
30 HINFO = 13,
32 MINFO = 14,
34 MX = 15,
36 TXT = 16,
38 AAAA = 28,
40 SRV = 33,
42 OPT = 41,
44}
45
46#[derive(Debug, PartialEq, Eq, Clone, Copy)]
50pub enum QueryType {
51 A = 1,
53 NS = 2,
55 MF = 4,
57 CNAME = 5,
59 SOA = 6,
61 MB = 7,
63 MG = 8,
65 MR = 9,
67 NULL = 10,
69 WKS = 11,
71 PTR = 12,
73 HINFO = 13,
75 MINFO = 14,
77 MX = 15,
79 TXT = 16,
81 AAAA = 28,
83 SRV = 33,
85 AXFR = 252,
87 MAILB = 253,
89 MAILA = 254,
91 All = 255,
93}
94
95
96#[derive(Debug, PartialEq, Eq, Clone, Copy)]
98pub enum Class {
99 IN = 1,
101 CS = 2,
104 CH = 3,
106 HS = 4,
108}
109
110#[derive(Debug, PartialEq, Eq, Clone, Copy)]
112pub enum QueryClass {
113 IN = 1,
115 CS = 2,
118 CH = 3,
120 HS = 4,
122 Any = 255,
124}
125
126#[derive(Debug, PartialEq, Eq, Clone, Copy)]
128pub enum Opcode {
129 StandardQuery,
130 InverseQuery,
131 ServerStatusRequest,
132 Reserved(u16),
133}
134
135#[derive(Debug, PartialEq, Eq, Clone, Copy)]
137pub enum ResponseCode {
138 NoError,
139 FormatError,
140 ServerFailure,
141 NameError,
142 NotImplemented,
143 Refused,
144 Reserved(u8),
145}
146
147impl From<u16> for Opcode {
148 fn from(code: u16) -> Opcode {
149 use self::Opcode::*;
150 match code {
151 0 => StandardQuery,
152 1 => InverseQuery,
153 2 => ServerStatusRequest,
154 x => Reserved(x),
155 }
156 }
157}
158impl Into<u16> for Opcode {
159 fn into(self) -> u16 {
160 use self::Opcode::*;
161 match self {
162 StandardQuery => 0,
163 InverseQuery => 1,
164 ServerStatusRequest => 2,
165 Reserved(x) => x,
166 }
167 }
168}
169
170impl From<u8> for ResponseCode {
171 fn from(code: u8) -> ResponseCode {
172 use self::ResponseCode::*;
173 match code {
174 0 => NoError,
175 1 => FormatError,
176 2 => ServerFailure,
177 3 => NameError,
178 4 => NotImplemented,
179 5 => Refused,
180 6...15 => Reserved(code),
181 x => panic!("Invalid response code {}", x),
182 }
183 }
184}
185impl Into<u8> for ResponseCode {
186 fn into(self) -> u8 {
187 use self::ResponseCode::*;
188 match self {
189 NoError => 0,
190 FormatError => 1,
191 ServerFailure => 2,
192 NameError => 3,
193 NotImplemented => 4,
194 Refused => 5,
195 Reserved(code) => code,
196 }
197 }
198}
199
200impl QueryType {
201 pub fn parse(code: u16) -> Result<QueryType, Error> {
202 use self::QueryType::*;
203 match code {
204 1 => Ok(A),
205 2 => Ok(NS),
206 4 => Ok(MF),
207 5 => Ok(CNAME),
208 6 => Ok(SOA),
209 7 => Ok(MB),
210 8 => Ok(MG),
211 9 => Ok(MR),
212 10 => Ok(NULL),
213 11 => Ok(WKS),
214 12 => Ok(PTR),
215 13 => Ok(HINFO),
216 14 => Ok(MINFO),
217 15 => Ok(MX),
218 16 => Ok(TXT),
219 28 => Ok(AAAA),
220 33 => Ok(SRV),
221 252 => Ok(AXFR),
222 253 => Ok(MAILB),
223 254 => Ok(MAILA),
224 255 => Ok(All),
225 x => Err(Error::InvalidQueryType(x)),
226 }
227 }
228}
229
230impl QueryClass {
231 pub fn parse(code: u16) -> Result<QueryClass, Error> {
232 use self::QueryClass::*;
233 match code {
234 1 => Ok(IN),
235 2 => Ok(CS),
236 3 => Ok(CH),
237 4 => Ok(HS),
238 255 => Ok(Any),
239 x => Err(Error::InvalidQueryClass(x)),
240 }
241 }
242}
243
244impl Type {
245 pub fn parse(code: u16) -> Result<Type, Error> {
246 use self::Type::*;
247 match code {
248 1 => Ok(A),
249 2 => Ok(NS),
250 4 => Ok(MF),
251 5 => Ok(CNAME),
252 6 => Ok(SOA),
253 7 => Ok(MB),
254 8 => Ok(MG),
255 9 => Ok(MR),
256 10 => Ok(NULL),
257 11 => Ok(WKS),
258 12 => Ok(PTR),
259 13 => Ok(HINFO),
260 14 => Ok(MINFO),
261 15 => Ok(MX),
262 16 => Ok(TXT),
263 28 => Ok(AAAA),
264 33 => Ok(SRV),
265 41 => Ok(OPT),
266 x => Err(Error::InvalidType(x)),
267 }
268 }
269}
270
271impl Class {
272 pub fn parse(code: u16) -> Result<Class, Error> {
273 use self::Class::*;
274 match code {
275 1 => Ok(IN),
276 2 => Ok(CS),
277 3 => Ok(CH),
278 4 => Ok(HS),
279 x => Err(Error::InvalidClass(x)),
280 }
281 }
282}