1use core::fmt::Display;
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub enum ParseErrorCode {
19 InvalidEOF,
20 InvalidNumberValue,
21 InvalidStringValue,
22 ExpectedSomeIdent,
23 ExpectedSomeValue,
24 ExpectedColon,
25 ExpectedArrayCommaOrEnd,
26 ExpectedObjectCommaOrEnd,
27 UnexpectedTrailingCharacters,
28 KeyMustBeAString,
29 ControlCharacterWhileParsingString,
30 InvalidEscaped(u8),
31 InvalidHex(u8),
32 InvalidLoneLeadingSurrogateInHexEscape(u16),
33 InvalidSurrogateInHexEscape(u16),
34 UnexpectedEndOfHexEscape,
35}
36
37impl Display for ParseErrorCode {
38 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
39 match *self {
40 ParseErrorCode::InvalidEOF => f.write_str("EOF while parsing a value"),
41 ParseErrorCode::InvalidNumberValue => f.write_str("invalid number"),
42 ParseErrorCode::InvalidStringValue => f.write_str("invalid string"),
43 ParseErrorCode::ExpectedSomeIdent => f.write_str("expected ident"),
44 ParseErrorCode::ExpectedSomeValue => f.write_str("expected value"),
45 ParseErrorCode::ExpectedColon => f.write_str("expected `:`"),
46 ParseErrorCode::ExpectedArrayCommaOrEnd => f.write_str("expected `,` or `]`"),
47 ParseErrorCode::ExpectedObjectCommaOrEnd => f.write_str("expected `,` or `}`"),
48 ParseErrorCode::UnexpectedTrailingCharacters => f.write_str("trailing characters"),
49 ParseErrorCode::KeyMustBeAString => f.write_str("key must be a string"),
50 ParseErrorCode::ControlCharacterWhileParsingString => {
51 f.write_str("control character (\\u0000-\\u001F) found while parsing a string")
52 }
53 ParseErrorCode::InvalidEscaped(n) => {
54 write!(f, "invalid escaped '{:X}'", n)
55 }
56 ParseErrorCode::InvalidHex(n) => {
57 write!(f, "invalid hex '{:X}'", n)
58 }
59 ParseErrorCode::InvalidLoneLeadingSurrogateInHexEscape(n) => {
60 write!(f, "lone leading surrogate in hex escape '{:X}'", n)
61 }
62 ParseErrorCode::InvalidSurrogateInHexEscape(n) => {
63 write!(f, "invalid surrogate in hex escape '{:X}'", n)
64 }
65 ParseErrorCode::UnexpectedEndOfHexEscape => f.write_str("unexpected end of hex escape"),
66 }
67 }
68}
69
70#[derive(Debug, Clone, PartialEq, Eq, Hash)]
71#[non_exhaustive]
72pub enum Error {
73 InvalidUtf8,
74 InvalidEOF,
75 InvalidToken,
76 InvalidCast,
77
78 InvalidJsonb,
79 InvalidJsonbHeader,
80 InvalidJsonbJEntry,
81
82 Syntax(ParseErrorCode, usize),
83}
84
85impl Display for Error {
86 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
87 match self {
88 Error::Syntax(code, pos) => write!(f, "{}, pos {}", code, pos),
89 _ => write!(f, "{:?}", self),
90 }
91 }
92}
93
94impl From<std::io::Error> for Error {
95 fn from(_error: std::io::Error) -> Self {
96 Error::InvalidUtf8
97 }
98}
99
100impl From<std::str::Utf8Error> for Error {
101 fn from(_error: std::str::Utf8Error) -> Self {
102 Error::InvalidUtf8
103 }
104}