1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use serde::{de, ser};
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{
format,
string::{self, String, ToString},
};
#[cfg(feature = "std")]
use std::{
error, format, io,
string::{self, String, ToString},
};
use core::{
fmt::{self, Display},
num, result,
};
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Deserialize(String),
EofWhileParsingValue,
ExpectedSomeValue,
FromUtf8Error(string::FromUtf8Error),
InvalidByteStrLen,
InvalidInteger,
InvalidDict,
InvalidList,
#[cfg(feature = "std")]
IoError(io::Error),
KeyMustBeAByteStr,
KeyWithoutValue,
ParseIntError(num::ParseIntError),
Serialize(String),
TrailingData,
UnsupportedType,
ValueWithoutKey,
}
#[cfg(feature = "std")]
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Deserialize(_) => None,
Error::EofWhileParsingValue => None,
Error::ExpectedSomeValue => None,
Error::FromUtf8Error(err) => Some(err),
Error::InvalidByteStrLen => None,
Error::InvalidInteger => None,
Error::InvalidDict => None,
Error::InvalidList => None,
#[cfg(feature = "std")]
Error::IoError(err) => Some(err),
Error::KeyMustBeAByteStr => None,
Error::KeyWithoutValue => None,
Error::ParseIntError(err) => Some(err),
Error::Serialize(_) => None,
Error::TrailingData => None,
Error::UnsupportedType => None,
Error::ValueWithoutKey => None,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Deserialize(str) => f.write_str(str),
Error::EofWhileParsingValue => f.write_str("eof while parsing value"),
Error::ExpectedSomeValue => f.write_str("expected some value"),
Error::FromUtf8Error(err) => Display::fmt(&*err, f),
Error::InvalidByteStrLen => f.write_str("invalid byte string length"),
Error::InvalidInteger => f.write_str("invalid integer"),
Error::InvalidDict => f.write_str("invalid dictionary"),
Error::InvalidList => f.write_str("invalid list"),
#[cfg(feature = "std")]
Error::IoError(err) => Display::fmt(&*err, f),
Error::KeyMustBeAByteStr => f.write_str("key must be a byte string"),
Error::KeyWithoutValue => f.write_str("key without value"),
Error::ParseIntError(err) => Display::fmt(&*err, f),
Error::Serialize(str) => f.write_str(str),
Error::TrailingData => f.write_str("trailing data error"),
Error::UnsupportedType => f.write_str("unsupported type"),
Error::ValueWithoutKey => f.write_str("value without key"),
}
}
}
#[cfg(feature = "std")]
impl From<Error> for io::Error {
fn from(other: Error) -> Self {
match other {
Error::IoError(e) => e,
_ => io::Error::from(io::ErrorKind::Other),
}
}
}
impl From<string::FromUtf8Error> for Error {
fn from(other: string::FromUtf8Error) -> Self {
Error::FromUtf8Error(other)
}
}
impl From<num::ParseIntError> for Error {
fn from(other: num::ParseIntError) -> Self {
Error::ParseIntError(other)
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Deserialize(msg.to_string())
}
fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
Error::Deserialize(format!(
"unexpected type error. invalid_type={}, expected_type={}",
unexp, exp
))
}
}
#[cfg(all(feature = "alloc", not(feature = "std")))]
impl de::StdError for Error {}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Serialize(msg.to_string())
}
}