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    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/// Parser error
154#[derive(Debug, PartialEq)]
155pub struct Error {
156    /// Byte index it was encountered at
157    index: usize,
158    /// Current character
159    character: Option<char>,
160    /// Type of error
161    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    /// Create a generic error
177    #[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    /// Returns the byte index the error occurred at.
187    #[must_use]
188    pub fn index(&self) -> usize {
189        self.index
190    }
191
192    /// Returns the current character the error occurred at.
193    #[must_use]
194    pub fn character(&self) -> Option<char> {
195        self.character
196    }
197
198    /// Returns the type of error that occurred.
199    #[must_use]
200    pub fn error(&self) -> &ErrorType {
201        &self.error
202    }
203
204    // These make it a bit easier to fit into a serde_json context
205    // The classification is based on that of serde_json - so if maybe
206    // you disagree with that classification, please leave as is anyway.
207
208    /// Indicates if the error that occurred was an IO error
209    #[must_use]
210    pub fn is_io(&self) -> bool {
211        // We have to include InternalError _somewhere_
212        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    /// Indicates if the error that occurred was an early EOF
220    #[must_use]
221    pub fn is_eof(&self) -> bool {
222        matches!(self.error, ErrorType::Eof)
223    }
224
225    /// Indicates if the error that occurred was due to a data shape error
226    #[must_use]
227    pub fn is_data(&self) -> bool {
228        // Lazy? maybe but if it aint something else...
229        !(self.is_syntax() || self.is_eof() || self.is_io())
230    }
231
232    /// Indicates if the error that occurred was due a JSON syntax error
233    #[must_use]
234    pub fn is_syntax(&self) -> bool {
235        // Lazy? maybe but if it aint something else...
236        matches!(
237            self.error,
238            ErrorType::InternalError(crate::InternalError::TapeError) | //This seems to get thrown on some syntax errors
239            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}