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 crate::command::Command;
use dbus_message_parser::Message;
use futures::channel::mpsc::TrySendError;
use futures::channel::oneshot::Canceled;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io::{Error as IoError, ErrorKind as IoErrorKind};

#[derive(Debug)]
pub enum DBusError {
    SendMessage(Message),
    AddPath(String),
    DeletePath(Option<String>),
    ListPath(String),
    AddInterface(String),
    AddSignalHandler(String),
    DeleteSignalHandler,
    RecvMessage(Option<Message>),
    Close,
}

impl From<TrySendError<Command>> for DBusError {
    fn from(e: TrySendError<Command>) -> Self {
        match e.into_inner() {
            Command::SendMessage(msg) => DBusError::SendMessage(msg),
            Command::SendMessageOneshot(msg, _) => DBusError::SendMessage(msg),
            Command::SendMessageMpcs(msg, _, _) => DBusError::SendMessage(msg),
            Command::AddPath(object_path, _) => DBusError::AddPath(object_path),
            Command::DeletePath(object_path) => DBusError::DeletePath(Some(object_path)),
            Command::DeleteSender(_) => DBusError::DeletePath(None),
            Command::DeleteReceiver(_) => DBusError::DeletePath(None),
            Command::ListPath(object_path, _) => DBusError::ListPath(object_path),
            Command::AddInterface(object_path, _) => DBusError::AddInterface(object_path),
            Command::AddSignalHandler(object_path, _) => DBusError::AddSignalHandler(object_path),
            Command::DeleteSignalHandler(_) => DBusError::DeleteSignalHandler,
            Command::ReceiveMessage(msg) => DBusError::RecvMessage(Some(msg)),
            Command::Close => DBusError::Close,
        }
    }
}

impl From<Canceled> for DBusError {
    fn from(_: Canceled) -> Self {
        DBusError::RecvMessage(None)
    }
}

impl From<DBusError> for IoError {
    fn from(e: DBusError) -> Self {
        IoError::new(IoErrorKind::Other, format!("call_hello: {:?}", e))
    }
}

impl Display for DBusError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            DBusError::SendMessage(msg) => write!(f, "Could not send message: {:?}", msg),
            DBusError::AddPath(path) => write!(f, "Could not add object to path: {}", path),
            DBusError::DeletePath(_object_path) => write!(f, "Could not delete object from path"),
            DBusError::ListPath(path) => write!(f, "Could not list object of path: {}", path),
            DBusError::AddInterface(interface) => {
                write!(f, "Could not add object to interface: {}", interface)
            }
            DBusError::AddSignalHandler(path) => {
                write!(f, "Could not add signal handler for path: {}", path)
            }
            DBusError::DeleteSignalHandler => write!(f, "Could not delete signal handler"),
            DBusError::RecvMessage(msg) => {
                write!(f, "Could not receive response for message: {:?}", msg)
            }
            DBusError::Close => write!(f, "Could not close DBus"),
        }
    }
}

pub type DBusResult<T> = std::result::Result<T, DBusError>;