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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::arg::TypeMismatchError;
use std::ffi::CString;
use std::{ptr, fmt};
use crate::{arg, to_c_str, c_str_to_slice, init_dbus, Message};
use crate::strings::ErrorName;
use std::error::Error as stdError;
pub struct Error {
e: ffi::DBusError,
}
unsafe impl Send for Error {}
unsafe impl Sync for Error {}
impl Error {
pub fn new_custom<'a, N: Into<ErrorName<'a>>>(name: N, message: &str) -> Error {
let n = to_c_str(&name.into());
let m = to_c_str(&message.replace("%","%%"));
let mut e = Error::empty();
unsafe { ffi::dbus_set_error(e.get_mut(), n.as_ptr(), m.as_ptr()) };
e
}
pub fn new_failed(message: &str) -> Error {
Error::new_custom("org.freedesktop.DBus.Error.Failed", message)
}
pub (crate) fn empty() -> Error {
init_dbus();
let mut e = ffi::DBusError {
name: ptr::null(),
message: ptr::null(),
dummy: 0,
padding1: ptr::null()
};
unsafe { ffi::dbus_error_init(&mut e); }
Error{ e: e }
}
pub fn name(&self) -> Option<&str> {
c_str_to_slice(&self.e.name)
}
pub fn message(&self) -> Option<&str> {
c_str_to_slice(&self.e.message)
}
pub (crate) fn get_mut(&mut self) -> &mut ffi::DBusError { &mut self.e }
}
impl Drop for Error {
fn drop(&mut self) {
unsafe { ffi::dbus_error_free(&mut self.e); }
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "D-Bus error: {} ({})", self.message().unwrap_or(""),
self.name().unwrap_or(""))
}
}
impl stdError for Error {
fn description(&self) -> &str { "D-Bus error" }
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
if let Some(x) = self.message() {
write!(f, "{}", x)
} else { Ok(()) }
}
}
impl From<arg::TypeMismatchError> for Error {
fn from(t: arg::TypeMismatchError) -> Error {
Error::new_custom("org.freedesktop.DBus.Error.Failed", &format!("{}", t))
}
}
impl From<MethodErr> for Error {
fn from(t: MethodErr) -> Error {
Error::new_custom(t.errorname(), t.description())
}
}
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct MethodErr(ErrorName<'static>, String);
impl MethodErr {
pub fn invalid_arg<T: fmt::Debug + ?Sized>(a: &T) -> MethodErr {
("org.freedesktop.DBus.Error.InvalidArgs", format!("Invalid argument {:?}", a)).into()
}
pub fn no_arg() -> MethodErr {
("org.freedesktop.DBus.Error.InvalidArgs", "Not enough arguments").into()
}
pub fn failed<T: fmt::Display + ?Sized>(a: &T) -> MethodErr {
("org.freedesktop.DBus.Error.Failed", a.to_string()).into()
}
pub fn no_path<T: fmt::Display + ?Sized>(a: &T) -> MethodErr {
("org.freedesktop.DBus.Error.UnknownObject", format!("Unknown object path {}", a)).into()
}
pub fn no_interface<T: fmt::Display + ?Sized>(a: &T) -> MethodErr {
("org.freedesktop.DBus.Error.UnknownInterface", format!("Unknown interface {}", a)).into()
}
pub fn no_method<T: fmt::Display + ?Sized>(a: &T) -> MethodErr {
("org.freedesktop.DBus.Error.UnknownMethod", format!("Unknown method {}", a)).into()
}
pub fn no_property<T: fmt::Display + ?Sized>(a: &T) -> MethodErr {
("org.freedesktop.DBus.Error.UnknownProperty", format!("Unknown property {}", a)).into()
}
pub fn ro_property<T: fmt::Display + ?Sized>(a: &T) -> MethodErr {
("org.freedesktop.DBus.Error.PropertyReadOnly", format!("Property {} is read only", a)).into()
}
pub fn errorname(&self) -> &ErrorName<'static> { &self.0 }
pub fn description(&self) -> &str { &self.1 }
pub fn to_message(&self, msg: &Message) -> Message {
msg.error(&self.0, &CString::new(&*self.1).unwrap())
}
}
impl fmt::Display for MethodErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl stdError for MethodErr {}
impl From<TypeMismatchError> for MethodErr {
fn from(t: TypeMismatchError) -> MethodErr { ("org.freedesktop.DBus.Error.Failed", format!("{}", t)).into() }
}
impl<T: Into<ErrorName<'static>>, M: Into<String>> From<(T, M)> for MethodErr {
fn from((t, m): (T, M)) -> MethodErr { MethodErr(t.into(), m.into()) }
}
impl From<Error> for MethodErr {
fn from(t: Error) -> MethodErr {
let n = t.name().unwrap_or("org.freedesktop.DBus.Error.Failed");
let m = t.message().unwrap_or("Unknown error");
MethodErr(String::from(n).into(), m.into())
}
}