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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::{fmt, io};

use thiserror::Error;

#[cfg(feature = "backtrace")]
use crate::trace;
use crate::{
    error::{ProtoError, ProtoErrorKind},
    rr::RecordType,
    serialize::txt::Token,
};
#[cfg(feature = "backtrace")]
#[cfg_attr(docsrs, doc(cfg(feature = "backtrace")))]
use backtrace::Backtrace as ExtBacktrace;

/// An alias for parse results returned by functions of this crate
pub type ParseResult<T> = ::std::result::Result<T, ParseError>;

/// The error kind for parse errors that get returned in the crate
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ParseErrorKind {
    /// An invalid numerical character was found
    #[error("invalid numerical character: {0}")]
    CharToInt(char),

    /// An error with an arbitrary message, referenced as &'static str
    #[error("{0}")]
    Message(&'static str),

    /// A token is missing
    #[error("token is missing: {0}")]
    MissingToken(String),

    /// An error with an arbitrary message, stored as String
    #[error("{0}")]
    Msg(String),

    /// A time string could not be parsed
    #[error("invalid time string: {0}")]
    ParseTime(String),

    /// Found an unexpected token in a stream
    #[error("unrecognized token in stream: {0:?}")]
    UnexpectedToken(Token),

    // foreign
    /// An address parse error
    #[error("network address parse error: {0}")]
    AddrParse(#[from] std::net::AddrParseError),

    /// A data encoding error
    #[error("data encoding error: {0}")]
    DataEncoding(#[from] data_encoding::DecodeError),

    /// An error got returned from IO
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),

    /// An error from the lexer
    #[error("lexer error: {0}")]
    Lexer(#[from] LexerError),

    /// A number parsing error
    #[error("error parsing number: {0}")]
    ParseInt(#[from] std::num::ParseIntError),

    /// An error got returned by the hickory-proto crate
    #[error("proto error: {0}")]
    Proto(#[from] ProtoError),

    /// Unknown RecordType
    #[error("unknown RecordType: {0}")]
    UnknownRecordType(u16),

    /// Unknown RecordType
    #[error("unsupported RecordType: {0}")]
    UnsupportedRecordType(RecordType),

    /// A request timed out
    #[error("request timed out")]
    Timeout,
}

impl Clone for ParseErrorKind {
    fn clone(&self) -> Self {
        use ParseErrorKind::*;
        match self {
            CharToInt(c) => CharToInt(*c),
            Message(msg) => Message(msg),
            MissingToken(ref s) => MissingToken(s.clone()),
            Msg(ref msg) => Msg(msg.clone()),
            ParseTime(ref s) => ParseTime(s.clone()),
            UnexpectedToken(ref token) => UnexpectedToken(token.clone()),

            AddrParse(e) => AddrParse(e.clone()),
            DataEncoding(e) => DataEncoding(*e),
            Io(e) => Io(std::io::Error::from(e.kind())),
            Lexer(e) => Lexer(e.clone()),
            ParseInt(e) => ParseInt(e.clone()),
            Proto(e) => Proto(e.clone()),
            UnsupportedRecordType(ty) => UnsupportedRecordType(*ty),
            UnknownRecordType(ty) => UnknownRecordType(*ty),
            Timeout => Timeout,
        }
    }
}

/// The error type for parse errors that get returned in the crate
#[derive(Error, Debug)]
pub struct ParseError {
    kind: ParseErrorKind,
    #[cfg(feature = "backtrace")]
    backtrack: Option<ExtBacktrace>,
}

impl ParseError {
    /// Get the kind of the error
    pub fn kind(&self) -> &ParseErrorKind {
        &self.kind
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        cfg_if::cfg_if! {
            if #[cfg(feature = "backtrace")] {
                if let Some(ref backtrace) = self.backtrack {
                    fmt::Display::fmt(&self.kind, f)?;
                    fmt::Debug::fmt(backtrace, f)
                } else {
                    fmt::Display::fmt(&self.kind, f)
                }
            } else {
                fmt::Display::fmt(&self.kind, f)
            }
        }
    }
}

impl From<ParseErrorKind> for ParseError {
    fn from(kind: ParseErrorKind) -> Self {
        Self {
            kind,
            #[cfg(feature = "backtrace")]
            backtrack: trace!(),
        }
    }
}

impl From<&'static str> for ParseError {
    fn from(msg: &'static str) -> Self {
        ParseErrorKind::Message(msg).into()
    }
}

impl From<String> for ParseError {
    fn from(msg: String) -> Self {
        ParseErrorKind::Msg(msg).into()
    }
}

impl From<std::net::AddrParseError> for ParseError {
    fn from(e: std::net::AddrParseError) -> Self {
        ParseErrorKind::from(e).into()
    }
}

impl From<::data_encoding::DecodeError> for ParseError {
    fn from(e: data_encoding::DecodeError) -> Self {
        ParseErrorKind::from(e).into()
    }
}

impl From<io::Error> for ParseError {
    fn from(e: io::Error) -> Self {
        match e.kind() {
            io::ErrorKind::TimedOut => ParseErrorKind::Timeout.into(),
            _ => ParseErrorKind::from(e).into(),
        }
    }
}

impl From<LexerError> for ParseError {
    fn from(e: LexerError) -> Self {
        ParseErrorKind::from(e).into()
    }
}

impl From<std::num::ParseIntError> for ParseError {
    fn from(e: std::num::ParseIntError) -> Self {
        ParseErrorKind::from(e).into()
    }
}

impl From<ProtoError> for ParseError {
    fn from(e: ProtoError) -> Self {
        match *e.kind() {
            ProtoErrorKind::Timeout => ParseErrorKind::Timeout.into(),
            _ => ParseErrorKind::from(e).into(),
        }
    }
}

impl From<std::convert::Infallible> for ParseError {
    fn from(_e: std::convert::Infallible) -> Self {
        panic!("infallible")
    }
}

impl From<ParseError> for io::Error {
    fn from(e: ParseError) -> Self {
        match *e.kind() {
            ParseErrorKind::Timeout => Self::new(io::ErrorKind::TimedOut, e),
            _ => Self::new(io::ErrorKind::Other, e),
        }
    }
}

/// An alias for lexer results returned by functions of this crate
pub(crate) type LexerResult<T> = ::std::result::Result<T, LexerError>;

/// The error kind for lexer errors that get returned in the crate
#[derive(Eq, PartialEq, Debug, Error, Clone)]
#[non_exhaustive]
pub enum LexerErrorKind {
    /// Unexpected end of input
    #[error("unexpected end of input")]
    EOF,

    /// An illegal character was found
    #[error("illegal character input: {0}")]
    IllegalCharacter(char),

    /// An illegal state was reached
    #[error("illegal state: {0}")]
    IllegalState(&'static str),

    /// An error with an arbitrary message, referenced as &'static str
    #[error("{0}")]
    Message(&'static str),

    /// An unclosed list was found
    #[error("unclosed list, missing ')'")]
    UnclosedList,

    /// An unclosed quoted string was found
    #[error("unclosed quoted string")]
    UnclosedQuotedString,

    /// An unrecognized character was found
    #[error("unrecognized character input: {0}")]
    UnrecognizedChar(char),

    /// An unrecognized dollar content was found
    #[error("unrecognized dollar content: {0}")]
    UnrecognizedDollar(String),

    /// An unrecognized octet was found
    #[error("unrecognized octet: {0:x}")]
    UnrecognizedOctet(u32),
}

/// The error type for lexer errors that get returned in the crate
#[derive(Clone, Error, Debug)]
pub struct LexerError {
    kind: LexerErrorKind,
    #[cfg(feature = "backtrace")]
    backtrack: Option<ExtBacktrace>,
}

impl LexerError {
    /// Get the kind of the error
    pub fn kind(&self) -> &LexerErrorKind {
        &self.kind
    }
}

impl From<LexerErrorKind> for LexerError {
    fn from(kind: LexerErrorKind) -> Self {
        Self {
            kind,
            #[cfg(feature = "backtrace")]
            backtrack: trace!(),
        }
    }
}

impl fmt::Display for LexerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        cfg_if::cfg_if! {
            if #[cfg(feature = "backtrace")] {
                if let Some(ref backtrace) = self.backtrack {
                    fmt::Display::fmt(&self.kind, f)?;
                    fmt::Debug::fmt(backtrace, f)
                } else {
                    fmt::Display::fmt(&self.kind, f)
                }
            } else {
                fmt::Display::fmt(&self.kind, f)
            }
        }
    }
}