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)]
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 #[allow(deprecated)]
120 fn description(&self) -> &str {
121 match *self {
122 Error::Syntax(..) => "syntax error",
123 Error::Io(ref error) => error.description(),
124 Error::FromUtf8(ref error) => error.description(),
125 }
126 }
127
128 fn cause(&self) -> Option<&dyn error::Error> {
129 match *self {
130 Error::Io(ref error) => Some(error),
131 Error::FromUtf8(ref error) => Some(error),
132 Error::Syntax(..) => None,
133 }
134 }
135}
136
137impl fmt::Display for Error {
138 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
139 match *self {
140 Error::Syntax(ref code, line, col) => {
141 write!(fmt, "{:?} at line {} column {}", code, line, col)
142 }
143 Error::Io(ref error) => fmt::Display::fmt(error, fmt),
144 Error::FromUtf8(ref error) => fmt::Display::fmt(error, fmt),
145 }
146 }
147}
148
149impl From<io::Error> for Error {
150 fn from(error: io::Error) -> Error {
151 Error::Io(error)
152 }
153}
154
155impl From<FromUtf8Error> for Error {
156 fn from(error: FromUtf8Error) -> Error {
157 Error::FromUtf8(error)
158 }
159}
160
161impl de::Error for Error {
162 fn custom<T: fmt::Display>(msg: T) -> Error {
163 Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
164 }
165}
166
167impl ser::Error for Error {
168 fn custom<T: fmt::Display>(msg: T) -> Error {
170 Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
171 }
172}
173
174pub type Result<T> = result::Result<T, Error>;