serde_json_wasm/de/
errors.rs1use alloc::string::{String, ToString};
2
3use serde::de;
4
5pub type Result<T> = core::result::Result<T, Error>;
7
8#[derive(Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum Error {
18 ControlCharacterInString,
20
21 EofWhileParsingList,
23
24 EofWhileParsingObject,
26
27 EofWhileParsingString,
29
30 EofWhileParsingValue,
32
33 ExpectedColon,
35
36 ExpectedHighSurrogate,
38
39 ExpectedListCommaOrEnd,
41
42 ExpectedLowSurrogate,
44
45 ExpectedObjectCommaOrEnd,
47
48 ExpectedSomeIdent,
50
51 ExpectedSomeValue,
53
54 InvalidEscape,
56
57 InvalidNumber,
59
60 InvalidType,
62
63 InvalidUnicodeCodePoint,
65
66 KeyMustBeAString,
68
69 LoneSurrogateFound,
71
72 TrailingCharacters,
74
75 TrailingComma,
77
78 RecursionLimitExceeded,
80
81 Custom(String),
83}
84
85impl de::StdError for Error {}
86
87impl de::Error for Error {
88 fn custom<T: core::fmt::Display>(msg: T) -> Self {
89 Error::Custom(msg.to_string())
90 }
91}
92
93impl core::fmt::Display for Error {
94 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
95 write!(
96 f,
97 "{}",
98 match self {
99 Error::ControlCharacterInString => "Control character found in string.",
100 Error::EofWhileParsingList => "EOF while parsing a list.",
101 Error::EofWhileParsingObject => "EOF while parsing an object.",
102 Error::EofWhileParsingString => "EOF while parsing a string.",
103 Error::EofWhileParsingValue => "EOF while parsing a JSON value.",
104 Error::ExpectedColon => "Expected this character to be a `':'`.",
105 Error::ExpectedHighSurrogate => "Expected a high surrogate (D800–DBFF).",
106 Error::ExpectedListCommaOrEnd => {
107 "Expected this character to be either a `','` or\
108 a \
109 `']'`."
110 }
111 Error::ExpectedLowSurrogate => "Expected a low surrogate (DC00–DFFF).",
112 Error::ExpectedObjectCommaOrEnd => {
113 "Expected this character to be either a `','` \
114 or a \
115 `'}'`."
116 }
117 Error::ExpectedSomeIdent => {
118 "Expected to parse either a `true`, `false`, or a \
119 `null`."
120 }
121 Error::ExpectedSomeValue => "Expected this character to start a JSON value.",
122 Error::InvalidEscape => "Invalid escape sequence.",
123 Error::InvalidNumber => "Invalid number.",
124 Error::InvalidType => "Invalid type",
125 Error::InvalidUnicodeCodePoint => "Invalid unicode code point.",
126 Error::KeyMustBeAString => "Object key is not a string.",
127 Error::LoneSurrogateFound => "Found a lone surrogate, which can exist in JSON but cannot be encoded to UTF-8.",
128 Error::TrailingCharacters => {
129 "JSON has non-whitespace trailing characters after \
130 the \
131 value."
132 }
133 Error::TrailingComma => "JSON has a comma after the last value in an array or map.",
134 Error::RecursionLimitExceeded => "JSON is nested too deeply, exceeded the recursion limit.",
135 Error::Custom(msg) => msg,
136 }
137 )
138 }
139}