1use std::fmt::Display;
2
3use serde::{de, ser};
4
5pub type SerResult<T> = std::result::Result<T, SerError>;
6pub type DeResult<T> = std::result::Result<T, DeError>;
7
8#[derive(Debug, Clone, PartialEq)]
9pub enum SerError {
10 Message(String),
11 FloatingPointNotSupported,
12 Io(String),
13 DictionaryKeyMustBeString,
14 FromUtf8Error(std::string::FromUtf8Error),
16 NoneNotSupported,
17}
18#[derive(Debug, Clone, PartialEq)]
19pub enum DeError {
20 Message(String),
21 UnexpectedEof,
22 SyntaxError(u8, Option<u8>),
24 ParseIntegerError(btoi::ParseIntegerError),
26 Utf8Error(std::str::Utf8Error),
28 ExpectedString,
29 ExpectedDictionary,
30 ExpectedEndOfDictionary,
31 ExpectedUnitStructName,
32 ExpectedInteger,
33 ExpectedCharString,
35}
36
37impl From<btoi::ParseIntegerError> for DeError {
38 fn from(e: btoi::ParseIntegerError) -> Self {
39 DeError::ParseIntegerError(e)
40 }
41}
42
43impl From<std::str::Utf8Error> for DeError {
44 fn from(x: std::str::Utf8Error) -> Self {
45 DeError::Utf8Error(x)
46 }
47}
48
49impl From<std::io::Error> for SerError {
50 fn from(ioe: std::io::Error) -> Self {
51 SerError::Io(ioe.to_string())
52 }
53}
54
55impl From<std::string::FromUtf8Error> for SerError {
56 fn from(ue: std::string::FromUtf8Error) -> Self {
57 SerError::FromUtf8Error(ue)
58 }
59}
60
61impl ser::Error for SerError {
62 fn custom<T>(msg: T) -> Self
63 where
64 T: std::fmt::Display,
65 {
66 SerError::Message(msg.to_string())
67 }
68}
69
70impl de::Error for DeError {
71 fn custom<T>(msg: T) -> Self
72 where
73 T: std::fmt::Display,
74 {
75 DeError::Message(msg.to_string())
76 }
77}
78
79impl Display for SerError {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 match self {
82 SerError::Message(s) => f.write_str(s),
83 SerError::Io(io) => f.write_str(io),
84 SerError::DictionaryKeyMustBeString => {
85 f.write_str("only byte strings allowed to be keys in dictionary")
86 }
87 SerError::FloatingPointNotSupported => {
88 f.write_str("floating point numbers are not supported")
89 }
90 SerError::FromUtf8Error(ue) => f.write_fmt(format_args!("{}", ue)),
91 SerError::NoneNotSupported => f.write_str("`None` variant of `Option` is not supported, perhaps you need `#[serde(skip_serializing_if = \"Option::is_none\")]`")
92 }
93 }
94}
95
96impl Display for DeError {
97 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98 match self {
99 DeError::Message(s) => f.write_str(s),
100 DeError::UnexpectedEof => f.write_str("unexpected EOF"),
101 DeError::SyntaxError(expected, got) => f.write_fmt(format_args!(
102 "syntax error: expected - {}, got - {:?}",
103 *expected as char, got
104 )),
105 DeError::ParseIntegerError(pie) => f.write_fmt(format_args!("{}", pie)),
106 DeError::Utf8Error(ue) => f.write_fmt(format_args!("{}", ue)),
107 DeError::ExpectedString => f.write_str("expected byte string"),
108 DeError::ExpectedDictionary => f.write_str("expected dictionary"),
109 DeError::ExpectedEndOfDictionary => f.write_str("expected enf of dictionary"),
110 DeError::ExpectedUnitStructName => f.write_str("expected name of the unit struct"),
111 DeError::ExpectedCharString => {
112 f.write_str("expected byte string with length at most 4 bytes")
113 }
114 DeError::ExpectedInteger => f.write_str("expected integer"),
115 }
116 }
117}
118
119impl std::error::Error for SerError {}
120impl std::error::Error for DeError {}