1use std::error;
7use std::fmt;
8use std::io;
9use std::result;
10use std::string::FromUtf8Error;
11
12use serde::de;
13use serde::ser;
14
15#[derive(Clone, PartialEq, Eq)]
17pub enum ErrorCode {
18 Custom(String),
20
21 EofWhileParsingList,
23
24 EofWhileParsingObject,
26
27 EofWhileParsingString,
29
30 EofWhileParsingValue,
32
33 ExpectedColon,
35
36 ExpectedListCommaOrEnd,
38
39 ExpectedObjectCommaOrEnd,
41
42 ExpectedSomeIdent,
44
45 ExpectedSomeValue,
47
48 InvalidEscape,
50
51 InvalidNumber,
53
54 InvalidUnicodeCodePoint,
56
57 KeyMustBeAString,
59
60 LoneLeadingSurrogateInHexEscape,
62
63 TrailingCharacters,
65
66 UnexpectedEndOfHexEscape,
68
69 PunctuatorInQlString,
71}
72
73impl fmt::Debug for ErrorCode {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 match *self {
78 ErrorCode::Custom(ref msg) => write!(f, "{msg}"),
79 ErrorCode::EofWhileParsingList => "EOF while parsing a list".fmt(f),
80 ErrorCode::EofWhileParsingObject => "EOF while parsing an object".fmt(f),
81 ErrorCode::EofWhileParsingString => "EOF while parsing a string".fmt(f),
82 ErrorCode::EofWhileParsingValue => "EOF while parsing a value".fmt(f),
83 ErrorCode::ExpectedColon => "expected `:`".fmt(f),
84 ErrorCode::ExpectedListCommaOrEnd => "expected `,` or `]`".fmt(f),
85 ErrorCode::ExpectedObjectCommaOrEnd => "expected `,` or `}`".fmt(f),
86 ErrorCode::ExpectedSomeIdent => "expected ident".fmt(f),
87 ErrorCode::ExpectedSomeValue => "expected value".fmt(f),
88 ErrorCode::InvalidEscape => "invalid escape".fmt(f),
89 ErrorCode::InvalidNumber => "invalid number".fmt(f),
90 ErrorCode::InvalidUnicodeCodePoint => "invalid Unicode code point".fmt(f),
91 ErrorCode::KeyMustBeAString => "key must be a string".fmt(f),
92 ErrorCode::LoneLeadingSurrogateInHexEscape => {
93 "lone leading surrogate in hex escape".fmt(f)
94 }
95 ErrorCode::TrailingCharacters => "trailing characters".fmt(f),
96 ErrorCode::UnexpectedEndOfHexEscape => "unexpected end of hex escape".fmt(f),
97 ErrorCode::PunctuatorInQlString => {
98 "found a punctuator character when expecting a quoteless string".fmt(f)
99 }
100 }
101 }
102}
103
104#[derive(Debug)]
107pub enum Error {
108 Syntax(ErrorCode, usize, usize),
110
111 Io(io::Error),
113
114 FromUtf8(FromUtf8Error),
116}
117
118impl error::Error for Error {
119 fn cause(&self) -> Option<&dyn error::Error> {
120 match *self {
121 Error::Io(ref error) => Some(error),
122 Error::FromUtf8(ref error) => Some(error),
123 _ => None,
124 }
125 }
126}
127
128impl fmt::Display for Error {
129 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
130 match *self {
131 Error::Syntax(ref code, line, col) => {
132 write!(fmt, "{code:?} at line {line} column {col}")
133 }
134 Error::Io(ref error) => fmt::Display::fmt(error, fmt),
135 Error::FromUtf8(ref error) => fmt::Display::fmt(error, fmt),
136 }
137 }
138}
139
140impl From<io::Error> for Error {
141 fn from(error: io::Error) -> Error {
142 Error::Io(error)
143 }
144}
145
146impl From<FromUtf8Error> for Error {
147 fn from(error: FromUtf8Error) -> Error {
148 Error::FromUtf8(error)
149 }
150}
151
152impl de::Error for Error {
153 fn custom<T: fmt::Display>(msg: T) -> Error {
154 Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
155 }
156}
157
158impl ser::Error for Error {
159 fn custom<T: fmt::Display>(msg: T) -> Error {
161 Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
162 }
163}
164
165pub type Result<T> = result::Result<T, Error>;