1pub type Result<T> = core::result::Result<T, Error>;
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4#[repr(i32)]
5pub enum Error {
7 ObStart = 1,
8 AttrStart = 2,
9 BadAttr = 3,
10 AttrLen = 4,
11 NoArray = 5,
12 NoBrak = 6,
13 StrLong = 7,
14 TokLong = 8,
15 BadTrail = 9,
16 ArrayStart = 10,
17 ObjArr = 11,
18 SubTooLong = 12,
19 BadSubTrail = 13,
20 SubType = 14,
21 BadString = 15,
22 CheckFail = 16,
23 NoParStr = 17,
24 BadEnum = 18,
25 QNonString = 19,
26 NonQString = 20,
27 Misc = 21,
28 BadNum = 22,
29 NullPtr = 23,
30 NoCurly = 24,
31 Empty = 25,
32 WriteLong = 26,
33 NestTooDeep = 27,
34 NestMismatch = 28,
35 BadSerialize = 29,
36}
37
38impl Error {
39 #[inline]
40 pub const fn message(self) -> &'static str {
41 match self {
42 Error::ObStart => "non-whitespace when expecting object start",
43 Error::AttrStart => "non-whitespace when expecting attribute start",
44 Error::BadAttr => "unknown attribute name",
45 Error::AttrLen => "attribute name too long",
46 Error::NoArray => "saw [ when not expecting array",
47 Error::NoBrak => "array element specified, but no [",
48 Error::StrLong => "string value too long",
49 Error::TokLong => "token value too long",
50 Error::BadTrail => "garbage while expecting comma or } or ]",
51 Error::ArrayStart => "didn't find expected array start",
52 Error::ObjArr => "error while parsing object array",
53 Error::SubTooLong => "too many array elements",
54 Error::BadSubTrail => "garbage while expecting array comma",
55 Error::SubType => "unsupported array element type",
56 Error::BadString => "error while string parsing",
57 Error::CheckFail => "check attribute not matched",
58 Error::NoParStr => "can't support strings in parallel arrays",
59 Error::BadEnum => "invalid enumerated value",
60 Error::QNonString => "saw quoted value when expecting nonstring",
61 Error::NonQString => "didn't see quoted value when expecting string",
62 Error::Misc => "other data conversion error",
63 Error::BadNum => "error while parsing a numerical argument",
64 Error::NullPtr => "unexpected null value or attribute pointer",
65 Error::NoCurly => "object element specified, but no {",
66 Error::Empty => "input was empty or white-space only",
67 Error::WriteLong => "JSON output buffer too small",
68 Error::NestTooDeep => "JSON nesting limit exceeded",
69 Error::NestMismatch => "JSON serializer nesting mismatch",
70 Error::BadSerialize => "invalid JSON serializer call sequence",
71 }
72 }
73}
74
75impl core::fmt::Display for Error {
76 #[inline]
77 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
78 f.write_str(self.message())
79 }
80}
81
82#[cfg(feature = "std")]
83impl std::error::Error for Error {}
84
85#[inline]
86pub fn error_string(err: Error) -> &'static str {
87 err.message()
88}