Skip to main content

simdjson_rust/
error.rs

1//! Simdjson error code translations and strong type definitions.
2
3use snafu::prelude::*;
4
5/// Strongly typed error enum representing C++ simdjson error codes.
6#[derive(Debug, Snafu, Clone, Copy, PartialEq, Eq)]
7pub enum SimdJsonError {
8    #[snafu(display("CAPACITY: This parser can't support a document that big"))]
9    Capacity,
10
11    #[snafu(display("MEMALLOC: Memory allocation failure"))]
12    MemAlloc,
13
14    #[snafu(display("TAPE_ERROR: The JSON is so badly formed we couldn't build a tape"))]
15    TapeError,
16
17    #[snafu(display("DEPTH_ERROR: Your document exceeds the maximum depth"))]
18    DepthError,
19
20    #[snafu(display("STRING_ERROR: Problem while parsing a string"))]
21    StringError,
22
23    #[snafu(display("T_ATOM_ERROR: Problem while parsing an atom starting with 't'"))]
24    TAtomError,
25
26    #[snafu(display("F_ATOM_ERROR: Problem while parsing an atom starting with 'f'"))]
27    FAtomError,
28
29    #[snafu(display("N_ATOM_ERROR: Problem while parsing an atom starting with 'n'"))]
30    NAtomError,
31
32    #[snafu(display("NUMBER_ERROR: Problem while parsing a number"))]
33    NumberError,
34
35    #[snafu(display("BIGINT_ERROR: Integer cannot be represented in 64 bits"))]
36    BigIntError,
37
38    #[snafu(display("UTF8_ERROR: The input is not valid UTF-8"))]
39    Utf8Error,
40
41    #[snafu(display("UNINITIALIZED: Uninitialized or an empty parser"))]
42    Uninitialized,
43
44    #[snafu(display("EMPTY: No structural element found"))]
45    Empty,
46
47    #[snafu(display("UNESCAPED_CHARS: Found unescaped characters in a string"))]
48    UnescapedChars,
49
50    #[snafu(display("UNCLOSED_STRING: Unclosed string"))]
51    UnclosedString,
52
53    #[snafu(display("UNSUPPORTED_ARCHITECTURE: Unsupported architecture"))]
54    UnsupportedArchitecture,
55
56    #[snafu(display("INCORRECT_TYPE: Element has the wrong type for this operation"))]
57    IncorrectType,
58
59    #[snafu(display("NUMBER_OUT_OF_RANGE: Number is too large or too small"))]
60    NumberOutOfRange,
61
62    #[snafu(display("INDEX_OUT_OF_BOUNDS: Array index is too large"))]
63    IndexOutOfBounds,
64
65    #[snafu(display("NO_SUCH_FIELD: The key was not found in the object"))]
66    NoSuchField,
67
68    #[snafu(display("IO_ERROR: Error reading file"))]
69    IoError,
70
71    #[snafu(display("INVALID_JSON_POINTER: Invalid JSON pointer syntax"))]
72    InvalidJsonPointer,
73
74    #[snafu(display("INVALID_URI_FRAGMENT: Fragment is not valid"))]
75    InvalidUriFragment,
76
77    #[snafu(display("UNEXPECTED_ERROR: Something went wrong, this is a bug in simdjson"))]
78    UnexpectedError,
79
80    #[snafu(display("PARSER_IN_USE"))]
81    ParserInUse,
82
83    #[snafu(display("OUT_OF_ORDER_ITERATION"))]
84    OutOfOrderIteration,
85
86    #[snafu(display("INSUFFICIENT_PADDING"))]
87    InsufficientPadding,
88
89    #[snafu(display("INCOMPLETE_ARRAY_OR_OBJECT"))]
90    IncompleteArrayOrObject,
91
92    #[snafu(display("SCALAR_DOCUMENT_AS_VALUE"))]
93    ScalarDocumentAsValue,
94
95    #[snafu(display("OUT_OF_BOUNDS"))]
96    OutOfBounds,
97
98    #[snafu(display("TRAILING_CONTENT"))]
99    TrailingContent,
100
101    #[snafu(display("OUT_OF_CAPACITY"))]
102    OutOfCapacity,
103
104    #[snafu(display("UNKNOWN_ERROR: Unknown error code {code}"))]
105    Unknown { code: i32 },
106}
107
108impl SimdJsonError {
109    /// Convert a raw C++ simdjson error code into a strongly typed
110    /// `SimdJsonError`.
111    ///
112    /// If the code is `0` (SUCCESS), this returns `None`. For all other codes,
113    /// this returns `Some(SimdJsonError)`.
114    pub fn from_code(code: i32) -> Option<Self> {
115        match code {
116            0 => None,
117            1 => Some(Self::Capacity),
118            2 => Some(Self::MemAlloc),
119            3 => Some(Self::TapeError),
120            4 => Some(Self::DepthError),
121            5 => Some(Self::StringError),
122            6 => Some(Self::TAtomError),
123            7 => Some(Self::FAtomError),
124            8 => Some(Self::NAtomError),
125            9 => Some(Self::NumberError),
126            10 => Some(Self::BigIntError),
127            11 => Some(Self::Utf8Error),
128            12 => Some(Self::Uninitialized),
129            13 => Some(Self::Empty),
130            14 => Some(Self::UnescapedChars),
131            15 => Some(Self::UnclosedString),
132            16 => Some(Self::UnsupportedArchitecture),
133            17 => Some(Self::IncorrectType),
134            18 => Some(Self::NumberOutOfRange),
135            19 => Some(Self::IndexOutOfBounds),
136            20 => Some(Self::NoSuchField),
137            21 => Some(Self::IoError),
138            22 => Some(Self::InvalidJsonPointer),
139            23 => Some(Self::InvalidUriFragment),
140            24 => Some(Self::UnexpectedError),
141            25 => Some(Self::ParserInUse),
142            26 => Some(Self::OutOfOrderIteration),
143            27 => Some(Self::InsufficientPadding),
144            28 => Some(Self::IncompleteArrayOrObject),
145            29 => Some(Self::ScalarDocumentAsValue),
146            30 => Some(Self::OutOfBounds),
147            31 => Some(Self::TrailingContent),
148            32 => Some(Self::OutOfCapacity),
149            other => Some(Self::Unknown { code: other }),
150        }
151    }
152
153    pub fn check_code(code: i32) -> Result<(), Self> {
154        match Self::from_code(code) {
155            Some(err) => Err(err),
156            None => Ok(()),
157        }
158    }
159}