Add error.rs
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
58d096ef13
commit
18178d81aa
2 changed files with 58 additions and 7 deletions
48
src/error.rs
Normal file
48
src/error.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright (C) 2022 Maxime “pep” Buquet <pep@bouah.net>
|
||||
//
|
||||
// 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 <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
//
|
||||
|
||||
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<dbus::Error> for Error {
|
||||
fn from(err: dbus::Error) -> Error {
|
||||
Error::DBusError(err)
|
||||
}
|
||||
}
|
17
src/main.rs
17
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<Self, DBusError> {
|
||||
pub fn connect() -> Result<Self, Error> {
|
||||
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<Self, DBusError> {
|
||||
pub fn connect() -> Result<Self, Error> {
|
||||
DBus::connect().map(|dbus| Self { dbus })
|
||||
}
|
||||
|
||||
pub async fn modems(&self) -> Result<Vec<Modem>, DBusError> {
|
||||
pub async fn modems(&self) -> Result<Vec<Modem>, 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<bool, DBusError> {
|
||||
pub async fn enabled(&self) -> Result<bool, Error> {
|
||||
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<String, DBusError> {
|
||||
ModemAccess::model(&self.dbus.proxy(&self.path)).await
|
||||
pub async fn model(&self) -> Result<String, Error> {
|
||||
ModemAccess::model(&self.dbus.proxy(&self.path)).await.map_err(Error::DBusError)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -178,7 +181,7 @@ impl From<i32> 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 {
|
||||
|
|
Loading…
Reference in a new issue