Skip to main content

hickory_proto/serialize/txt/
errors.rs

1use alloc::string::String;
2#[cfg(feature = "std")]
3use std::io;
4
5use thiserror::Error;
6
7use crate::{
8    error::ProtoError,
9    rr::RecordType,
10    serialize::{binary::DecodeError, txt::Token},
11};
12
13/// An alias for parse results returned by functions of this crate
14pub type ParseResult<T> = ::core::result::Result<T, ParseError>;
15
16/// The error kind for parse errors that get returned in the crate
17#[derive(Debug, Error)]
18#[non_exhaustive]
19pub enum ParseError {
20    /// An invalid numerical character was found
21    #[error("invalid numerical character: {0}")]
22    CharToInt(char),
23
24    /// An error with an arbitrary message, referenced as &'static str
25    #[error("{0}")]
26    Message(&'static str),
27
28    /// A token is missing
29    #[error("token is missing: {0}")]
30    MissingToken(String),
31
32    /// An error with an arbitrary message, stored as String
33    #[error("{0}")]
34    Msg(String),
35
36    /// A time string could not be parsed
37    #[error("invalid time string: {0}")]
38    ParseTime(String),
39
40    /// Found an unexpected token in a stream
41    #[error("unrecognized token in stream: {0:?}")]
42    UnexpectedToken(Token),
43
44    // foreign
45    /// An address parse error
46    #[error("network address parse error: {0}")]
47    AddrParse(#[from] core::net::AddrParseError),
48
49    /// A data encoding error
50    #[error("data encoding error: {0}")]
51    DataEncoding(#[cfg_attr(feature = "std", source)] data_encoding::DecodeError),
52
53    /// An error got returned from IO
54    #[cfg(feature = "std")]
55    #[error("io error: {0}")]
56    Io(#[from] io::Error),
57
58    /// An error from the lexer
59    #[error("lexer error: {0}")]
60    Lexer(#[from] LexerError),
61
62    /// A number parsing error
63    #[error("error parsing number: {0}")]
64    ParseInt(#[from] core::num::ParseIntError),
65
66    /// An error got returned by the hickory-proto crate
67    #[error("proto error: {0}")]
68    Proto(#[from] ProtoError),
69
70    /// Unknown RecordType
71    #[error("unknown RecordType: {0}")]
72    UnknownRecordType(u16),
73
74    /// Unknown RecordType
75    #[error("unsupported RecordType: {0}")]
76    UnsupportedRecordType(RecordType),
77}
78
79impl Clone for ParseError {
80    fn clone(&self) -> Self {
81        use ParseError::*;
82        match self {
83            CharToInt(c) => CharToInt(*c),
84            Message(msg) => Message(msg),
85            MissingToken(s) => MissingToken(s.clone()),
86            Msg(msg) => Msg(msg.clone()),
87            ParseTime(s) => ParseTime(s.clone()),
88            UnexpectedToken(token) => UnexpectedToken(token.clone()),
89
90            AddrParse(e) => AddrParse(e.clone()),
91            DataEncoding(e) => DataEncoding(*e),
92            #[cfg(feature = "std")]
93            Io(e) => Io(io::Error::from(e.kind())),
94            Lexer(e) => Lexer(e.clone()),
95            ParseInt(e) => ParseInt(e.clone()),
96            Proto(e) => Proto(e.clone()),
97            UnsupportedRecordType(ty) => UnsupportedRecordType(*ty),
98            UnknownRecordType(ty) => UnknownRecordType(*ty),
99        }
100    }
101}
102
103impl From<data_encoding::DecodeError> for ParseError {
104    fn from(e: data_encoding::DecodeError) -> Self {
105        Self::DataEncoding(e)
106    }
107}
108
109impl From<&'static str> for ParseError {
110    fn from(msg: &'static str) -> Self {
111        Self::Message(msg)
112    }
113}
114
115impl From<String> for ParseError {
116    fn from(msg: String) -> Self {
117        Self::Msg(msg)
118    }
119}
120
121impl From<DecodeError> for ParseError {
122    fn from(e: DecodeError) -> Self {
123        Self::from(ProtoError::from(e))
124    }
125}
126
127impl From<core::convert::Infallible> for ParseError {
128    fn from(_e: core::convert::Infallible) -> Self {
129        panic!("infallible")
130    }
131}
132
133#[cfg(feature = "std")]
134impl From<ParseError> for io::Error {
135    fn from(e: ParseError) -> Self {
136        Self::other(e)
137    }
138}
139
140/// An alias for lexer results returned by functions of this crate
141pub(crate) type LexerResult<T> = Result<T, LexerError>;
142
143/// The error kind for lexer errors that get returned in the crate
144#[derive(Eq, PartialEq, Debug, Error, Clone)]
145#[non_exhaustive]
146pub enum LexerError {
147    /// Unexpected end of input
148    #[error("unexpected end of input")]
149    EOF,
150
151    /// An illegal character was found
152    #[error("illegal character input: {0}")]
153    IllegalCharacter(char),
154
155    /// An illegal state was reached
156    #[error("illegal state: {0}")]
157    IllegalState(&'static str),
158
159    /// An error with an arbitrary message, referenced as &'static str
160    #[error("{0}")]
161    Message(&'static str),
162
163    /// An unclosed list was found
164    #[error("unclosed list, missing ')'")]
165    UnclosedList,
166
167    /// An unclosed quoted string was found
168    #[error("unclosed quoted string")]
169    UnclosedQuotedString,
170
171    /// An unrecognized character was found
172    #[error("unrecognized character input: {0}")]
173    UnrecognizedChar(char),
174
175    /// An unrecognized dollar content was found
176    #[error("unrecognized dollar content: {0}")]
177    UnrecognizedDollar(String),
178
179    /// An unrecognized octet was found
180    #[error("unrecognized octet: {0:x}")]
181    UnrecognizedOctet(u32),
182}