hickory_proto/serialize/txt/
errors.rs1use 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
13pub type ParseResult<T> = ::core::result::Result<T, ParseError>;
15
16#[derive(Debug, Error)]
18#[non_exhaustive]
19pub enum ParseError {
20 #[error("invalid numerical character: {0}")]
22 CharToInt(char),
23
24 #[error("{0}")]
26 Message(&'static str),
27
28 #[error("token is missing: {0}")]
30 MissingToken(String),
31
32 #[error("{0}")]
34 Msg(String),
35
36 #[error("invalid time string: {0}")]
38 ParseTime(String),
39
40 #[error("unrecognized token in stream: {0:?}")]
42 UnexpectedToken(Token),
43
44 #[error("network address parse error: {0}")]
47 AddrParse(#[from] core::net::AddrParseError),
48
49 #[error("data encoding error: {0}")]
51 DataEncoding(#[cfg_attr(feature = "std", source)] data_encoding::DecodeError),
52
53 #[cfg(feature = "std")]
55 #[error("io error: {0}")]
56 Io(#[from] io::Error),
57
58 #[error("lexer error: {0}")]
60 Lexer(#[from] LexerError),
61
62 #[error("error parsing number: {0}")]
64 ParseInt(#[from] core::num::ParseIntError),
65
66 #[error("proto error: {0}")]
68 Proto(#[from] ProtoError),
69
70 #[error("unknown RecordType: {0}")]
72 UnknownRecordType(u16),
73
74 #[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
140pub(crate) type LexerResult<T> = Result<T, LexerError>;
142
143#[derive(Eq, PartialEq, Debug, Error, Clone)]
145#[non_exhaustive]
146pub enum LexerError {
147 #[error("unexpected end of input")]
149 EOF,
150
151 #[error("illegal character input: {0}")]
153 IllegalCharacter(char),
154
155 #[error("illegal state: {0}")]
157 IllegalState(&'static str),
158
159 #[error("{0}")]
161 Message(&'static str),
162
163 #[error("unclosed list, missing ')'")]
165 UnclosedList,
166
167 #[error("unclosed quoted string")]
169 UnclosedQuotedString,
170
171 #[error("unrecognized character input: {0}")]
173 UnrecognizedChar(char),
174
175 #[error("unrecognized dollar content: {0}")]
177 UnrecognizedDollar(String),
178
179 #[error("unrecognized octet: {0:x}")]
181 UnrecognizedOctet(u32),
182}