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
use std::fmt::Display;
#[derive(Debug, PartialEq)]
pub enum Error {
Message(String),
IO(String),
InvalidTag(u8),
NonunicodeString(Vec<u8>),
NoRootCompound,
InvalidSize(i32),
}
pub type Result<T> = std::result::Result<T, Error>;
impl serde::de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Message(msg) => f.write_str(msg),
Error::IO(e) => f.write_fmt(format_args!("{}", e)),
Error::InvalidTag(tag) => f.write_fmt(format_args!("Invalid tag number: {}", tag)),
Error::NonunicodeString(_) => f.write_str("string or name was not valid utf-8"),
Error::NoRootCompound => {
f.write_str("require a root compound to start deserialization")
}
Error::InvalidSize(size) => f.write_fmt(format_args!("list size was {}", size)),
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::IO(err.to_string())
}
}
impl std::error::Error for Error {}