Implement From<ServiceError> for dbus::MethodErr

This commit is contained in:
Elias Schriefer 2020-11-05 19:11:00 +01:00
parent baa1d67f29
commit 7495c57013
2 changed files with 23 additions and 4 deletions

View File

@ -10,6 +10,19 @@ pub enum ServiceError {
IOError(std::io::Error)
}
impl ServiceError {
pub fn name(&self) -> String {
match self {
Self::NoPermission => crate::iface_name("Error.NoPermission"),
Self::NotImplementedYet => crate::iface_name("Error.NotImplementedYet"),
Self::AlreadyStarted => crate::iface_name("Error.AlreadyStarted"),
Self::AlreadyStopped => crate::iface_name("Error.AlreadyStopped"),
Self::Failed => crate::iface_name("Error.Failed"),
Self::IOError(err) => format!("{}.{:?}", crate::iface_name("Error.IO"), err.kind()),
}
}
}
impl Error for ServiceError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
@ -39,6 +52,12 @@ impl core::fmt::Display for ServiceError {
}
}
impl From<ServiceError> for dbus::MethodErr {
fn from(se: ServiceError) -> Self {
(se.name(), se.to_string()).into()
}
}
#[derive(Debug, Copy, Clone)]
pub enum Service {
Daemon,

View File

@ -3,7 +3,7 @@ use dbus::{
message::MatchRule,
channel::MatchingReceiver
};
use dbus_tree::{ Factory, MethodErr };
use dbus_tree::Factory;
use ifaces::*;
use tokio::sync::RwLock;
use std::sync::Arc;
@ -58,19 +58,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
data.read().await
.start().await
.map(|_| vec![m.msg.method_return()])
.map_err(|err| MethodErr::failed(&err))
.map_err(|err| err.into())
})))
.add_m(f.method_sync("Stop", (), handle_iface!(m, |data| {
data.read().await
.stop().await
.map(|_| vec![m.msg.method_return()])
.map_err(|err| MethodErr::failed(&err))
.map_err(|err| err.into())
})))
.add_m(f.method_sync("Restart", (), handle_iface!(m, |data| {
data.read().await
.restart().await
.map(|_| vec![m.msg.method_return()])
.map_err(|err| MethodErr::failed(&err))
.map_err(|err| err.into())
})))
.add_m(f.method_sync("IsRunning", (), handle_iface!(m, |data| {
Ok(vec![m.msg.method_return().append1(