diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..51d71e5 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,48 @@ +// Copyright (C) 2022 Maxime “pep” Buquet +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +// + +use std::convert::From; +use std::error::Error as StdError; + +use mmdbus::dbus; + +#[derive(Debug)] +pub enum Error { + DBusError(dbus::Error), +} + +impl StdError for Error { + fn cause(&self) -> Option<&dyn StdError> { + match self { + Error::DBusError(e) => Some(e), + } + } +} + +impl std::fmt::Display for Error { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Error::DBusError(e) => write!(fmt, "dbus error: {}", e), + } + } +} + +impl From for Error { + fn from(err: dbus::Error) -> Error { + Error::DBusError(err) + } +} diff --git a/src/main.rs b/src/main.rs index 1132457..70925d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,10 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +pub mod error; pub mod mm; + +use error::Error; use mm::modem::Modem as ModemAccess; use std::fmt; @@ -53,7 +56,7 @@ pub struct DBus { } impl DBus { - pub fn connect() -> Result { + pub fn connect() -> Result { let (resource, conn) = connection::new_system_sync()?; let handle: JoinHandle<()> = tokio::spawn(async { @@ -78,11 +81,11 @@ pub struct ModemManager { } impl ModemManager { - pub fn connect() -> Result { + pub fn connect() -> Result { DBus::connect().map(|dbus| Self { dbus }) } - pub async fn modems(&self) -> Result, DBusError> { + pub async fn modems(&self) -> Result, Error> { let objects = self.dbus.proxy(MM_PATH).get_managed_objects().await?; let modems = objects .into_iter() @@ -111,7 +114,7 @@ impl fmt::Debug for Modem { } impl Modem { - pub async fn enabled(&self) -> Result { + pub async fn enabled(&self) -> Result { let state: ModemState = ModemAccess::state(&self.dbus.proxy(&self.path)).await.map(Into::into)?; Ok(match state { ModemState::Enabling @@ -124,8 +127,8 @@ impl Modem { }) } - pub async fn model(&self) -> Result { - ModemAccess::model(&self.dbus.proxy(&self.path)).await + pub async fn model(&self) -> Result { + ModemAccess::model(&self.dbus.proxy(&self.path)).await.map_err(Error::DBusError) } } @@ -178,7 +181,7 @@ impl From for ModemState { } } -async fn print_foo(mm: ModemManager) -> Result<(), DBusError> { +async fn print_foo(mm: ModemManager) -> Result<(), Error> { let modems = mm.modems().await?; println!("Modems: {:?}", modems); for modem in modems {