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
use std::fmt::Display;
use serde::{de, ser};
pub type SerResult<T> = std::result::Result<T, SerError>;
pub type DeResult<T> = std::result::Result<T, DeError>;
#[derive(Debug, Clone, PartialEq)]
pub enum SerError {
Message(String),
FloatingPointNotSupported,
Io(String),
DictionaryKeyMustBeString,
FromUtf8Error(std::string::FromUtf8Error),
NoneNotSupported,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DeError {
Message(String),
UnexpectedEof,
SyntaxError(u8, Option<u8>),
ParseIntegerError(btoi::ParseIntegerError),
Utf8Error(std::str::Utf8Error),
ExpectedString,
ExpectedDictionary,
ExpectedEndOfDictionary,
ExpectedUnitStructName,
ExpectedInteger,
ExpectedCharString,
}
impl From<btoi::ParseIntegerError> for DeError {
fn from(e: btoi::ParseIntegerError) -> Self {
DeError::ParseIntegerError(e)
}
}
impl From<std::str::Utf8Error> for DeError {
fn from(x: std::str::Utf8Error) -> Self {
DeError::Utf8Error(x)
}
}
impl From<std::io::Error> for SerError {
fn from(ioe: std::io::Error) -> Self {
SerError::Io(ioe.to_string())
}
}
impl From<std::string::FromUtf8Error> for SerError {
fn from(ue: std::string::FromUtf8Error) -> Self {
SerError::FromUtf8Error(ue)
}
}
impl ser::Error for SerError {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
SerError::Message(msg.to_string())
}
}
impl de::Error for DeError {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
DeError::Message(msg.to_string())
}
}
impl Display for SerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SerError::Message(s) => f.write_str(s),
SerError::Io(io) => f.write_str(io),
SerError::DictionaryKeyMustBeString => {
f.write_str("only byte strings allowed to be keys in dictionary")
}
SerError::FloatingPointNotSupported => {
f.write_str("floating point numbers are not supported")
}
SerError::FromUtf8Error(ue) => f.write_fmt(format_args!("{}", ue)),
SerError::NoneNotSupported => f.write_str("`None` variant of `Option` is not supported, perhaps you need `#[serde(skip_serializing_if = \"Option::is_none\")]`")
}
}
}
impl Display for DeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DeError::Message(s) => f.write_str(s),
DeError::UnexpectedEof => f.write_str("unexpected EOF"),
DeError::SyntaxError(expected, got) => f.write_fmt(format_args!(
"syntax error: expected - {}, got - {:?}",
*expected as char, got
)),
DeError::ParseIntegerError(pie) => f.write_fmt(format_args!("{}", pie)),
DeError::Utf8Error(ue) => f.write_fmt(format_args!("{}", ue)),
DeError::ExpectedString => f.write_str("expected byte string"),
DeError::ExpectedDictionary => f.write_str("expected dictionary"),
DeError::ExpectedEndOfDictionary => f.write_str("expected enf of dictionary"),
DeError::ExpectedUnitStructName => f.write_str("expected name of the unit struct"),
DeError::ExpectedCharString => {
f.write_str("expected byte string with length at most 4 bytes")
}
DeError::ExpectedInteger => f.write_str("expected integer"),
}
}
}
impl std::error::Error for SerError {}
impl std::error::Error for DeError {}