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
use std::ptr;
use crate::{tree, arg, to_c_str, c_str_to_slice, init_dbus};
use crate::strings::ErrorName;
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 std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "D-Bus error: {} ({})", self.message().unwrap_or(""),
self.name().unwrap_or(""))
}
}
impl std::error::Error for Error {
fn description(&self) -> &str { "D-Bus error" }
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(),std::fmt::Error> {
if let Some(x) = self.message() {
write!(f, "{:?}", x.to_string())
} 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<tree::MethodErr> for Error {
fn from(t: tree::MethodErr) -> Error {
Error::new_custom(t.errorname(), t.description())
}
}