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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use serde::{de, ser};
use static_assertions::assert_impl_all;
use std::{convert::Infallible, error, fmt, result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MaxDepthExceeded {
Structure,
Array,
Container,
}
impl fmt::Display for MaxDepthExceeded {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Structure => write!(
f,
"Maximum allowed depth for structures in encoding was exceeded"
),
Self::Array => write!(
f,
"Maximum allowed depth for arrays in encoding was exceeded"
),
Self::Container => write!(
f,
"Maximum allowed depth for containers in encoding was exceeded"
),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Message(String),
Io(std::io::Error),
IncorrectType,
Utf8(std::str::Utf8Error),
PaddingNot0(u8),
UnknownFd,
MissingFramingOffset,
IncompatibleFormat(crate::Signature<'static>, crate::EncodingFormat),
SignatureMismatch(crate::Signature<'static>, String),
OutOfBounds,
MaxDepthExceeded(MaxDepthExceeded),
}
assert_impl_all!(Error: Send, Sync, Unpin);
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Error::Message(msg), Error::Message(other)) => msg == other,
(Error::IncorrectType, Error::IncorrectType) => true,
(Error::Utf8(msg), Error::Utf8(other)) => msg == other,
(Error::PaddingNot0(p), Error::PaddingNot0(other)) => p == other,
(Error::UnknownFd, Error::UnknownFd) => true,
(Error::MaxDepthExceeded(max1), Error::MaxDepthExceeded(max2)) => max1 == max2,
(_, _) => false,
}
}
}
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"),
Error::MissingFramingOffset => write!(
f,
"Missing framing offset at the end of GVariant-encoded container"
),
Error::IncompatibleFormat(sig, format) => write!(
f,
"Type `{}` is not compatible with `{}` format",
sig, format,
),
Error::SignatureMismatch(provided, expected) => write!(
f,
"Signature mismatch: got `{}`, expected {}",
provided, expected,
),
Error::OutOfBounds => write!(
f,
"Out of bounds range specified",
),
Error::MaxDepthExceeded(max) => write!(f, "{max}"),
}
}
}
impl From<Infallible> for Error {
fn from(i: Infallible) -> Self {
match i {}
}
}
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>;