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
use std::{error, fmt, io};
use zvariant::Error as VariantError;
use crate::{Message, MessageError, MessageType};
#[derive(Debug)]
pub enum Error {
Address(String),
Io(io::Error),
Message(MessageError),
Variant(VariantError),
Handshake(String),
InvalidReply,
MethodError(String, Option<String>, Message),
InvalidGUID,
Unsupported,
NoTLSConnection,
NoTLSNode,
}
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::Address(_) => None,
Error::Io(e) => Some(e),
Error::Handshake(_) => None,
Error::Message(e) => Some(e),
Error::Variant(e) => Some(e),
Error::InvalidReply => None,
Error::MethodError(_, _, _) => None,
Error::InvalidGUID => None,
Error::Unsupported => None,
Error::NoTLSConnection => None,
Error::NoTLSNode => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Address(e) => write!(f, "address error: {}", e),
Error::Io(e) => write!(f, "I/O error: {}", e),
Error::Handshake(e) => write!(f, "D-Bus handshake failed: {}", e),
Error::Message(e) => write!(f, "Message creation error: {}", e),
Error::Variant(e) => write!(f, "{}", e),
Error::InvalidReply => write!(f, "Invalid D-Bus method reply"),
Error::MethodError(name, detail, _reply) => write!(
f,
"{}: {}",
name,
detail.as_ref().map(|s| s.as_str()).unwrap_or("no details")
),
Error::InvalidGUID => write!(f, "Invalid GUID"),
Error::Unsupported => write!(f, "Connection support is lacking"),
Error::NoTLSConnection => write!(f, "No TLS connection"),
Error::NoTLSNode => write!(f, "No TLS node"),
}
}
}
impl From<io::Error> for Error {
fn from(val: io::Error) -> Self {
Error::Io(val)
}
}
impl From<MessageError> for Error {
fn from(val: MessageError) -> Self {
Error::Message(val)
}
}
impl From<VariantError> for Error {
fn from(val: VariantError) -> Self {
Error::Variant(val)
}
}
impl From<Message> for Error {
fn from(message: Message) -> Error {
let header = match message.header() {
Ok(header) => header,
Err(e) => {
return Error::Message(e);
}
};
if header.primary().msg_type() != MessageType::Error {
return Error::InvalidReply;
}
if let Ok(Some(name)) = header.error_name() {
let name = String::from(name);
match message.body_unchecked::<&str>() {
Ok(detail) => Error::MethodError(name, Some(String::from(detail)), message),
Err(_) => Error::MethodError(name, None, message),
}
} else {
Error::InvalidReply
}
}
}
pub type Result<T> = std::result::Result<T, Error>;