1use std::fmt;
2
3use value_trait::ValueType;
4
5#[derive(Debug)]
7pub enum ErrorType {
8 Unexpected(Option<ValueType>, Option<ValueType>),
10 InputTooLarge,
13 BadKeyType,
15 ExpectedArray,
17 ExpectedArrayComma,
19 ExpectedBoolean,
21 ExpectedEnum,
23 ExpectedFloat,
25 ExpectedInteger,
27 ExpectedMap,
29 ExpectedObjectColon,
31 ExpectedMapComma,
33 ExpectedMapEnd,
35 ExpectedNull,
37 ExpectedTrue,
39 ExpectedFalse,
41 ExpectedNumber,
43 ExpectedSigned,
45 ExpectedString,
47 ExpectedUnsigned,
49 InternalError(InternalError),
51 InvalidEscape,
53 InvalidExponent,
55 InvalidNumber,
57 InvalidUtf8,
59 InvalidUnicodeEscape,
61 InvalidUnicodeCodepoint,
63 KeyMustBeAString,
65 NoStructure,
67 Parser,
69 Eof,
71 Serde(String),
73 Syntax,
75 TrailingData,
77 UnexpectedCharacter,
79 UnterminatedString,
81 ExpectedArrayContent,
83 ExpectedObjectContent,
85 ExpectedObjectKey,
87 Overflow,
89 SimdUnsupported,
91 Io(std::io::Error),
93}
94
95#[derive(Clone, Debug, PartialEq)]
96pub enum InternalError {
97 TapeError,
98}
99
100impl From<std::io::Error> for Error {
101 fn from(e: std::io::Error) -> Self {
102 Self::generic(ErrorType::Io(e))
103 }
104}
105
106#[cfg(not(tarpaulin_include))]
107impl PartialEq for ErrorType {
108 fn eq(&self, other: &Self) -> bool {
109 match (self, other) {
110 (Self::Io(_), Self::Io(_))
111 | (Self::BadKeyType, Self::BadKeyType)
112 | (Self::ExpectedArray, Self::ExpectedArray)
113 | (Self::ExpectedArrayComma, Self::ExpectedArrayComma)
114 | (Self::ExpectedBoolean, Self::ExpectedBoolean)
115 | (Self::ExpectedTrue, Self::ExpectedTrue)
116 | (Self::ExpectedFalse, Self::ExpectedFalse)
117 | (Self::ExpectedEnum, Self::ExpectedEnum)
118 | (Self::ExpectedFloat, Self::ExpectedFloat)
119 | (Self::ExpectedInteger, Self::ExpectedInteger)
120 | (Self::ExpectedMap, Self::ExpectedMap)
121 | (Self::ExpectedObjectColon, Self::ExpectedObjectColon)
122 | (Self::ExpectedMapComma, Self::ExpectedMapComma)
123 | (Self::ExpectedMapEnd, Self::ExpectedMapEnd)
124 | (Self::ExpectedNull, Self::ExpectedNull)
125 | (Self::ExpectedNumber, Self::ExpectedNumber)
126 | (Self::ExpectedSigned, Self::ExpectedSigned)
127 | (Self::ExpectedString, Self::ExpectedString)
128 | (Self::ExpectedUnsigned, Self::ExpectedUnsigned)
129 | (Self::InvalidEscape, Self::InvalidEscape)
130 | (Self::InvalidExponent, Self::InvalidExponent)
131 | (Self::InvalidNumber, Self::InvalidNumber)
132 | (Self::InvalidUtf8, Self::InvalidUtf8)
133 | (Self::InvalidUnicodeEscape, Self::InvalidUnicodeEscape)
134 | (Self::InvalidUnicodeCodepoint, Self::InvalidUnicodeCodepoint)
135 | (Self::KeyMustBeAString, Self::KeyMustBeAString)
136 | (Self::NoStructure, Self::NoStructure)
137 | (Self::Parser, Self::Parser)
138 | (Self::Eof, Self::Eof)
139 | (Self::Syntax, Self::Syntax)
140 | (Self::TrailingData, Self::TrailingData)
141 | (Self::UnexpectedCharacter, Self::UnexpectedCharacter)
142 | (Self::UnterminatedString, Self::UnterminatedString)
143 | (Self::ExpectedArrayContent, Self::ExpectedArrayContent)
144 | (Self::ExpectedObjectContent, Self::ExpectedObjectContent)
145 | (Self::ExpectedObjectKey, Self::ExpectedObjectKey)
146 | (Self::Overflow, Self::Overflow) => true,
147 (Self::Serde(s1), Self::Serde(s2)) => s1 == s2,
148 (Self::InternalError(e1), Self::InternalError(e2)) => e1 == e2,
149 _ => false,
150 }
151 }
152}
153#[derive(Debug, PartialEq)]
155pub struct Error {
156 index: usize,
158 character: Option<char>,
160 error: ErrorType,
162}
163
164impl Error {
165 pub(crate) fn new(index: usize, character: Option<char>, error: ErrorType) -> Self {
166 Self {
167 index,
168 character,
169 error,
170 }
171 }
172 pub(crate) fn new_c(index: usize, character: char, error: ErrorType) -> Self {
173 Self::new(index, Some(character), error)
174 }
175
176 #[must_use = "Error creation"]
178 pub fn generic(t: ErrorType) -> Self {
179 Self {
180 index: 0,
181 character: None,
182 error: t,
183 }
184 }
185
186 #[must_use]
188 pub fn index(&self) -> usize {
189 self.index
190 }
191
192 #[must_use]
194 pub fn character(&self) -> Option<char> {
195 self.character
196 }
197
198 #[must_use]
200 pub fn error(&self) -> &ErrorType {
201 &self.error
202 }
203
204 #[must_use]
210 pub fn is_io(&self) -> bool {
211 match &self.error {
213 ErrorType::Io(_) | ErrorType::InputTooLarge => true,
214 ErrorType::InternalError(e) if !matches!(e, crate::InternalError::TapeError) => true,
215 _ => false,
216 }
217 }
218
219 #[must_use]
221 pub fn is_eof(&self) -> bool {
222 matches!(self.error, ErrorType::Eof)
223 }
224
225 #[must_use]
227 pub fn is_data(&self) -> bool {
228 !(self.is_syntax() || self.is_eof() || self.is_io())
230 }
231
232 #[must_use]
234 pub fn is_syntax(&self) -> bool {
235 matches!(
237 self.error,
238 ErrorType::InternalError(crate::InternalError::TapeError) | ErrorType::BadKeyType |
240 ErrorType::ExpectedArrayComma |
241 ErrorType::ExpectedObjectColon |
242 ErrorType::ExpectedMapComma |
243 ErrorType::ExpectedMapEnd |
244 ErrorType::InvalidEscape |
245 ErrorType::InvalidExponent |
246 ErrorType::InvalidNumber |
247 ErrorType::InvalidUtf8 |
248 ErrorType::InvalidUnicodeEscape |
249 ErrorType::InvalidUnicodeCodepoint |
250 ErrorType::KeyMustBeAString |
251 ErrorType::NoStructure |
252 ErrorType::Parser |
253 ErrorType::Syntax |
254 ErrorType::TrailingData |
255 ErrorType::UnexpectedCharacter |
256 ErrorType::UnterminatedString |
257 ErrorType::ExpectedArrayContent |
258 ErrorType::ExpectedObjectContent |
259 ErrorType::ExpectedObjectKey |
260 ErrorType::Overflow |
261 ErrorType::SimdUnsupported
262 )
263 }
264}
265impl std::error::Error for Error {}
266
267#[cfg(not(tarpaulin_include))]
268impl fmt::Display for Error {
269 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
270 if let Some(c) = self.character {
271 write!(f, "{:?} at character {} ('{c}')", self.error, self.index)
272 } else {
273 write!(f, "{:?} at character {}", self.error, self.index)
274 }
275 }
276}
277
278#[cfg(not(tarpaulin_include))]
279impl From<Error> for std::io::Error {
280 fn from(e: Error) -> Self {
281 std::io::Error::new(std::io::ErrorKind::InvalidData, e)
282 }
283}