1use std::fmt::{self, Display};
2
3#[derive(Debug)]
4pub enum Error {
5 SerializeError(String),
6 DeserializeError(String),
7 TrailingCharacters,
8 Eof,
9 ExpectedSlash,
10 ExpectedInfinity,
11 ExpectedNaN,
12 ExpectedFraction,
13 ExpectedExponent,
14 ExpectedDecimalDigit,
15 ExpectedHexadecimalDigit,
16 ExpectedOpenBracket,
17 ExpectedCloseBracket,
18 ExpectedComma,
19 ExpectedColon,
20 ExpectedDouble(i64),
21 ExpectedNil,
22 ExpectedTrue,
23 ExpectedFalse,
24 Expected(String),
25 UnexpectedUnderBar,
26 UnexpectedSign,
27 UnexpectedLineBreak(char),
28 UnexpectedOpenBracket,
29 UnicodeConversionError(u32),
30 Base64DecodeError,
31}
32
33impl Display for Error {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 use Error::*;
36 match self {
37 SerializeError(s) => write!(f, "serialization error: {}", s),
38 DeserializeError(s) => write!(f, "deserialization error: {}", s),
39 TrailingCharacters => write!(f, "input has trailing characters"),
40 Eof => write!(f, "encountered EOF while parsing"),
41 ExpectedSlash => write!(f, "expected a slash"),
42 ExpectedInfinity => write!(f, "expected Infinity"),
43 ExpectedNaN => write!(f, "expected NaN"),
44 ExpectedFraction => write!(f, "expected fractional part"),
45 ExpectedExponent => write!(f, "expected exponent part"),
46 ExpectedDecimalDigit => write!(f, "expected decimal digit"),
47 ExpectedHexadecimalDigit => write!(f, "expected hexadecimal digit"),
48 ExpectedOpenBracket => write!(f, "expected open bracket"),
49 ExpectedCloseBracket => write!(f, "expected close bracket"),
50 ExpectedComma => write!(f, "expected comma"),
51 ExpectedColon => write!(f, "expected colon"),
52 ExpectedDouble(x) => write!(f, "expected double: {}", x),
53 ExpectedNil => write!(f, "expected nil"),
54 ExpectedTrue => write!(f, "expected true"),
55 ExpectedFalse => write!(f, "expected false"),
56 Expected(s) => write!(f, "expected: {}", s),
57 UnexpectedUnderBar => write!(f, "unexpected under bar"),
58 UnexpectedSign => write!(f, "unexpected sign"),
59 UnexpectedLineBreak(c) => write!(f, "unexpected line break: {}", c.escape_debug()),
60 UnexpectedOpenBracket => write!(f, "unexpected open bracket"),
61 UnicodeConversionError(x) => write!(f, "failed to convert into unicode: {}", x),
62 Base64DecodeError => write!(f, "failed to decode base64"),
63 }
64 }
65}
66
67impl ::std::error::Error for Error {}
68
69impl ::serde::ser::Error for Error {
70 fn custom<T: Display>(v: T) -> Error {
71 Error::SerializeError(v.to_string())
72 }
73}
74
75impl ::serde::de::Error for Error {
76 fn custom<T: Display>(v: T) -> Error {
77 Error::DeserializeError(v.to_string())
78 }
79}