simd_json/
error.rs

1use std::fmt;
2
3use value_trait::ValueType;
4
5/// Error types encountered while parsing
6#[derive(Debug)]
7pub enum ErrorType {
8    /// A specific type was expected but another one encountered.
9    Unexpected(Option<ValueType>, Option<ValueType>),
10    /// Simd-json only supports inputs of up to
11    /// 4GB in size.
12    InputTooLarge,
13    /// The key of a map isn't a string
14    BadKeyType,
15    /// Expected an array
16    ExpectedArray,
17    /// Expected a `,` in an array
18    ExpectedArrayComma,
19    /// expected an boolean
20    ExpectedBoolean,
21    /// Expected an enum
22    ExpectedEnum,
23    /// Expected a float
24    ExpectedFloat,
25    /// Expected an integer
26    ExpectedInteger,
27    /// Expected a map
28    ExpectedMap,
29    /// Expected an `:` to separate key and value in an object
30    ExpectedObjectColon,
31    /// Expected a `,` in an object
32    ExpectedMapComma,
33    /// Expected the object to end
34    ExpectedMapEnd,
35    /// Expected a null
36    ExpectedNull,
37    /// Expected a true
38    ExpectedTrue,
39    /// Expected a false
40    ExpectedFalse,
41    /// Expected a number
42    ExpectedNumber,
43    /// Expected a signed number
44    ExpectedSigned,
45    /// Expected a string
46    ExpectedString,
47    /// Expected an unsigned number
48    ExpectedUnsigned,
49    /// Internal error
50    InternalError(InternalError),
51    /// Invalid escape sequence
52    InvalidEscape,
53    /// Invalid exponent in a floating point number
54    InvalidExponent,
55    /// Invalid number
56    InvalidNumber,
57    /// Invalid UTF8 codepoint
58    InvalidUtf8,
59    /// Invalid Unicode escape sequence
60    InvalidUnicodeEscape,
61    /// Invalid Unicode codepoint
62    InvalidUnicodeCodepoint,
63    /// Object Key isn't a string
64    KeyMustBeAString,
65    /// Non structural character
66    NoStructure,
67    /// Parser Error
68    Parser,
69    /// Early End Of File
70    Eof,
71    /// Generic serde error
72    Serde(String),
73    /// Generic syntax error
74    Syntax,
75    /// Trailing data
76    TrailingData,
77    /// Unexpected character
78    UnexpectedCharacter,
79    /// Unterminated string
80    UnterminatedString,
81    /// Expected Array elements
82    ExpectedArrayContent,
83    /// Expected Object elements
84    ExpectedObjectContent,
85    /// Expected Object Key
86    ExpectedObjectKey,
87    /// Overflow of a limited buffer
88    Overflow,
89    /// No SIMD support detected during runtime
90    SimdUnsupported,
91    /// IO error
92    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/// Parser error
155#[derive(Debug, PartialEq)]
156pub struct Error {
157    /// Byte index it was encountered at
158    index: usize,
159    /// Current character
160    character: Option<char>,
161    /// Type of error
162    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    /// Create a generic error
178    #[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    /// Returns the byte index the error occurred at.
188    #[must_use]
189    pub fn index(&self) -> usize {
190        self.index
191    }
192
193    /// Returns the current character the error occurred at.
194    #[must_use]
195    pub fn character(&self) -> Option<char> {
196        self.character
197    }
198
199    /// Returns the type of error that occurred.
200    #[must_use]
201    pub fn error(&self) -> &ErrorType {
202        &self.error
203    }
204
205    // These make it a bit easier to fit into a serde_json context
206    // The classification is based on that of serde_json - so if maybe
207    // you disagree with that classification, please leave as is anyway.
208
209    /// Indicates if the error that occurred was an IO error
210    #[must_use]
211    pub fn is_io(&self) -> bool {
212        // We have to include InternalError _somewhere_
213        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    /// Indicates if the error that occurred was an early EOF
221    #[must_use]
222    pub fn is_eof(&self) -> bool {
223        matches!(self.error, ErrorType::Eof)
224    }
225
226    /// Indicates if the error that occurred was due to a data shape error
227    #[must_use]
228    pub fn is_data(&self) -> bool {
229        // Lazy? maybe but if it aint something else...
230        !(self.is_syntax() || self.is_eof() || self.is_io())
231    }
232
233    /// Indicates if the error that occurred was due a JSON syntax error
234    #[must_use]
235    pub fn is_syntax(&self) -> bool {
236        // Lazy? maybe but if it aint something else...
237        matches!(
238            self.error,
239            ErrorType::InternalError(crate::InternalError::TapeError) | //This seems to get thrown on some syntax errors
240            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}