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
use serde::{de, ser};
use std::{error, fmt, result};
#[derive(Debug)]
pub enum Error {
Message(String),
Io(std::io::Error),
IncorrectType,
Utf8(std::str::Utf8Error),
PaddingNot0(u8),
UnknownFd,
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match self {
Error::Io(_) => false,
_ => self == other,
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
Error::Utf8(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Message(s) => write!(f, "{}", s),
Error::Io(e) => e.fmt(f),
Error::IncorrectType => write!(f, "incorrect type"),
Error::Utf8(e) => write!(f, "{}", e),
Error::PaddingNot0(b) => write!(f, "Unexpected non-0 padding byte `{}`", b),
Error::UnknownFd => write!(f, "File descriptor not in the given FD index"),
}
}
}
impl de::Error for Error {
fn custom<T>(msg: T) -> Error
where
T: fmt::Display,
{
Error::Message(msg.to_string())
}
}
impl ser::Error for Error {
fn custom<T>(msg: T) -> Error
where
T: fmt::Display,
{
Error::Message(msg.to_string())
}
}
pub type Result<T> = result::Result<T, Error>;