1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use serde::de;
use std::fmt::Display;

use thiserror::Error;

pub type SimdJsonResult<T> = Result<T, SimdJsonError>;

#[derive(Debug, Error)]
pub enum SimdJsonError {
    #[error("Message: {0}")]
    Message(String),

    #[error("This parser can't support a document that big")]
    Capacity,

    #[error("Error allocating memory, we're most likely out of memory")]
    MemAlloc,

    #[error("Something went wrong while writing to the tape")]
    TapeError,

    #[error("The JSON document was too deep (too many nested objects and arrays)")]
    DepthError,

    #[error("Problem while parsing a string")]
    StringError,

    #[error("Problem while parsing an atom starting with the letter 't'")]
    TAtomError,

    #[error("Problem while parsing an atom starting with the letter 'f'")]
    FAtomError,

    #[error("Problem while parsing an atom starting with the letter 'n'")]
    NAtomError,

    #[error("Problem while parsing a number")]
    NumberError,

    #[error("The input is not valid UTF-8")]
    Utf8Error,

    #[error("Uninitialized")]
    Uninitialized,

    #[error("Empty: no JSON found")]
    Empty,

    #[error("Within strings, some characters must be escaped, we found unescaped characters")]
    UnescapedChars,

    #[error("A string is opened, but never closed.")]
    UnclosedString,

    #[error("simdjson does not have an implementation supported by this CPU architecture (perhaps it's a non-SIMD CPU?).")]
    UnsupportedArchitecture,

    #[error("The JSON element does not have the requested type.")]
    IncorrectType,

    #[error("The JSON number is too large or too small to fit within the requested type.")]
    NumberOutOfRange,

    #[error("Attempted to access an element of a JSON array that is beyond its length.")]
    IndexOutOfBounds,

    #[error("The JSON field referenced does not exist in this object.")]
    NoSuchField,

    #[error("Error reading the file.")]
    IoError,

    #[error("Invalid JSON pointer syntax.")]
    InvalidJsonPointer,

    #[error("Invalid URI fragment syntax.")]
    InvalidUriFragment,

    #[error(
        "Unexpected error, consider reporting this problem as you may have found a bug in simdjson"
    )]
    UnexpectedError,
}

impl From<i32> for SimdJsonError {
    fn from(error_code: i32) -> Self {
        match error_code {
            0 => panic!("No error"),
            1 => panic!("No error and buffer still has more data"),
            2 => SimdJsonError::Capacity,
            3 => SimdJsonError::MemAlloc,
            4 => SimdJsonError::TapeError,
            5 => SimdJsonError::DepthError,
            6 => SimdJsonError::StringError,
            7 => SimdJsonError::TAtomError,
            8 => SimdJsonError::FAtomError,
            9 => SimdJsonError::NAtomError,
            10 => SimdJsonError::NumberError,
            11 => SimdJsonError::Utf8Error,
            12 => SimdJsonError::Uninitialized,
            13 => SimdJsonError::Empty,
            14 => SimdJsonError::UnescapedChars,
            15 => SimdJsonError::UnclosedString,
            16 => SimdJsonError::UnsupportedArchitecture,
            17 => SimdJsonError::IncorrectType,
            18 => SimdJsonError::NumberOutOfRange,
            19 => SimdJsonError::IndexOutOfBounds,
            20 => SimdJsonError::NoSuchField,
            21 => SimdJsonError::IoError,
            22 => SimdJsonError::InvalidJsonPointer,
            23 => SimdJsonError::InvalidUriFragment,
            24 => SimdJsonError::UnexpectedError,
            x => panic!("Unknown error code: {}", x),
        }
    }
}

impl de::Error for SimdJsonError {
    fn custom<T>(msg: T) -> Self
    where
        T: Display,
    {
        Self::Message(msg.to_string())
    }
}