diff --git a/src/clients/mod.rs b/src/clients/mod.rs index 81e44b6..a74310b 100644 --- a/src/clients/mod.rs +++ b/src/clients/mod.rs @@ -164,12 +164,17 @@ impl Clients { } #[cfg(feature = "upower")] - pub fn upower(&mut self) -> Arc> { - self.upower - .get_or_insert_with(|| { - crate::await_sync(async { upower::create_display_proxy().await }) - }) - .clone() + pub fn upower(&mut self) -> ClientResult> { + let client = match &self.upower { + Some(client) => client.clone(), + None => { + let client = await_sync(async { upower::create_display_proxy().await })?; + self.upower.replace(client.clone()); + client + } + }; + + Ok(client) } #[cfg(feature = "volume")] diff --git a/src/clients/upower.rs b/src/clients/upower.rs index f3cade1..d597044 100644 --- a/src/clients/upower.rs +++ b/src/clients/upower.rs @@ -1,21 +1,15 @@ -use crate::register_client; +use crate::clients::ClientResult; +use crate::register_fallible_client; use std::sync::Arc; use upower_dbus::UPowerProxy; use zbus::fdo::PropertiesProxy; -pub async fn create_display_proxy() -> Arc> { - let dbus = Box::pin(zbus::Connection::system()) - .await - .expect("failed to create connection to system bus"); +pub async fn create_display_proxy() -> ClientResult> { + let dbus = Box::pin(zbus::Connection::system()).await?; - let device_proxy = UPowerProxy::new(&dbus) - .await - .expect("failed to create upower proxy"); + let device_proxy = UPowerProxy::new(&dbus).await?; - let display_device = device_proxy - .get_display_device() - .await - .unwrap_or_else(|_| panic!("failed to get display device for {device_proxy:?}")); + let display_device = device_proxy.get_display_device().await?; let path = display_device.path().to_owned(); @@ -26,10 +20,9 @@ pub async fn create_display_proxy() -> Arc> { .expect("failed to set proxy path") .cache_properties(zbus::CacheProperties::No) .build() - .await - .expect("failed to build proxy"); + .await?; - Arc::new(proxy) + Ok(Arc::new(proxy)) } -register_client!(PropertiesProxy<'static>, upower); +register_fallible_client!(PropertiesProxy<'static>, upower); diff --git a/src/main.rs b/src/main.rs index 43a81d3..e2aac18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,7 +78,10 @@ fn run_with_args() { #[cfg(feature = "schema")] if args.print_schema { let schema = schemars::schema_for!(Config); - println!("{}", serde_json::to_string_pretty(&schema).expect("to be serializable")); + println!( + "{}", + serde_json::to_string_pretty(&schema).expect("to be serializable") + ); return; } diff --git a/src/modules/upower.rs b/src/modules/upower.rs index d461cc3..ec881d9 100644 --- a/src/modules/upower.rs +++ b/src/modules/upower.rs @@ -73,7 +73,7 @@ impl Module for UpowerModule { ) -> Result<()> { let tx = context.tx.clone(); - let display_proxy = context.client::(); + let display_proxy = context.try_client::()?; spawn(async move { let mut prop_changed_stream = display_proxy.receive_properties_changed().await?;