diff --git a/sfsmcd/src/ifaces.rs b/sfsmcd/src/ifaces.rs index e8ecc37..bbaa168 100644 --- a/sfsmcd/src/ifaces.rs +++ b/sfsmcd/src/ifaces.rs @@ -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 for dbus::MethodErr { + fn from(se: ServiceError) -> Self { + (se.name(), se.to_string()).into() + } +} + #[derive(Debug, Copy, Clone)] pub enum Service { Daemon, diff --git a/sfsmcd/src/main.rs b/sfsmcd/src/main.rs index 52cb987..dc9ad82 100644 --- a/sfsmcd/src/main.rs +++ b/sfsmcd/src/main.rs @@ -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> { 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(