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 #[must_use]
109 fn eq(&self, other: &Self) -> bool {
110 match (self, other) {
111 (Self::Io(_), Self::Io(_))
112 | (Self::BadKeyType, Self::BadKeyType)
113 | (Self::ExpectedArray, Self::ExpectedArray)
114 | (Self::ExpectedArrayComma, Self::ExpectedArrayComma)
115 | (Self::ExpectedBoolean, Self::ExpectedBoolean)
116 | (Self::ExpectedTrue, Self::ExpectedTrue)
117 | (Self::ExpectedFalse, Self::ExpectedFalse)
118 | (Self::ExpectedEnum, Self::ExpectedEnum)
119 | (Self::ExpectedFloat, Self::ExpectedFloat)
120 | (Self::ExpectedInteger, Self::ExpectedInteger)
121 | (Self::ExpectedMap, Self::ExpectedMap)
122 | (Self::ExpectedObjectColon, Self::ExpectedObjectColon)
123 | (Self::ExpectedMapComma, Self::ExpectedMapComma)
124 | (Self::ExpectedMapEnd, Self::ExpectedMapEnd)
125 | (Self::ExpectedNull, Self::ExpectedNull)
126 | (Self::ExpectedNumber, Self::ExpectedNumber)
127 | (Self::ExpectedSigned, Self::ExpectedSigned)
128 | (Self::ExpectedString, Self::ExpectedString)
129 | (Self::ExpectedUnsigned, Self::ExpectedUnsigned)
130 | (Self::InvalidEscape, Self::InvalidEscape)
131 | (Self::InvalidExponent, Self::InvalidExponent)
132 | (Self::InvalidNumber, Self::InvalidNumber)
133 | (Self::InvalidUtf8, Self::InvalidUtf8)
134 | (Self::InvalidUnicodeEscape, Self::InvalidUnicodeEscape)
135 | (Self::InvalidUnicodeCodepoint, Self::InvalidUnicodeCodepoint)
136 | (Self::KeyMustBeAString, Self::KeyMustBeAString)
137 | (Self::NoStructure, Self::NoStructure)
138 | (Self::Parser, Self::Parser)
139 | (Self::Eof, Self::Eof)
140 | (Self::Syntax, Self::Syntax)
141 | (Self::TrailingData, Self::TrailingData)
142 | (Self::UnexpectedCharacter, Self::UnexpectedCharacter)
143 | (Self::UnterminatedString, Self::UnterminatedString)
144 | (Self::ExpectedArrayContent, Self::ExpectedArrayContent)
145 | (Self::ExpectedObjectContent, Self::ExpectedObjectContent)
146 | (Self::ExpectedObjectKey, Self::ExpectedObjectKey)
147 | (Self::Overflow, Self::Overflow) => true,
148 (Self::Serde(s1), Self::Serde(s2)) => s1 == s2,
149 (Self::InternalError(e1), Self::InternalError(e2)) => e1 == e2,
150 _ => false,
151 }
152 }
153}
154#[derive(Debug, PartialEq)]
156pub struct Error {
157 index: usize,
159 character: Option<char>,
161 error: ErrorType,
163}
164
165impl Error {
166 pub(crate) fn new(index: usize, character: Option<char>, error: ErrorType) -> Self {
167 Self {
168 index,
169 character,
170 error,
171 }
172 }
173 pub(crate) fn new_c(index: usize, character: char, error: ErrorType) -> Self {
174 Self::new(index, Some(character), error)
175 }
176
177 #[must_use = "Error creation"]
179 pub fn generic(t: ErrorType) -> Self {
180 Self {
181 index: 0,
182 character: None,
183 error: t,
184 }
185 }
186
187 #[must_use]
189 pub fn index(&self) -> usize {
190 self.index
191 }
192
193 #[must_use]
195 pub fn character(&self) -> Option<char> {
196 self.character
197 }
198
199 #[must_use]
201 pub fn error(&self) -> &ErrorType {
202 &self.error
203 }
204
205 #[must_use]
211 pub fn is_io(&self) -> bool {
212 match &self.error {
214 ErrorType::Io(_) | ErrorType::InputTooLarge => true,
215 ErrorType::InternalError(e) if !matches!(e, crate::InternalError::TapeError) => true,
216 _ => false,
217 }
218 }
219
220 #[must_use]
222 pub fn is_eof(&self) -> bool {
223 matches!(self.error, ErrorType::Eof)
224 }
225
226 #[must_use]
228 pub fn is_data(&self) -> bool {
229 !(self.is_syntax() || self.is_eof() || self.is_io())
231 }
232
233 #[must_use]
235 pub fn is_syntax(&self) -> bool {
236 matches!(
238 self.error,
239 ErrorType::InternalError(crate::InternalError::TapeError) | ErrorType::BadKeyType |
241 ErrorType::ExpectedArrayComma |
242 ErrorType::ExpectedObjectColon |
243 ErrorType::ExpectedMapComma |
244 ErrorType::ExpectedMapEnd |
245 ErrorType::InvalidEscape |
246 ErrorType::InvalidExponent |
247 ErrorType::InvalidNumber |
248 ErrorType::InvalidUtf8 |
249 ErrorType::InvalidUnicodeEscape |
250 ErrorType::InvalidUnicodeCodepoint |
251 ErrorType::KeyMustBeAString |
252 ErrorType::NoStructure |
253 ErrorType::Parser |
254 ErrorType::Syntax |
255 ErrorType::TrailingData |
256 ErrorType::UnexpectedCharacter |
257 ErrorType::UnterminatedString |
258 ErrorType::ExpectedArrayContent |
259 ErrorType::ExpectedObjectContent |
260 ErrorType::ExpectedObjectKey |
261 ErrorType::Overflow |
262 ErrorType::SimdUnsupported
263 )
264 }
265}
266impl std::error::Error for Error {}
267
268#[cfg(not(tarpaulin_include))]
269impl fmt::Display for Error {
270 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
271 if let Some(c) = self.character {
272 write!(f, "{:?} at character {} ('{c}')", self.error, self.index)
273 } else {
274 write!(f, "{:?} at character {}", self.error, self.index)
275 }
276 }
277}
278
279#[cfg(not(tarpaulin_include))]
280impl From<Error> for std::io::Error {
281 fn from(e: Error) -> Self {
282 std::io::Error::new(std::io::ErrorKind::InvalidData, e)
283 }
284}