Skip to content

Commit

Permalink
fix(upower): avoid panic on client init error
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Aug 9, 2024
1 parent 9d12535 commit 474e1fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
17 changes: 11 additions & 6 deletions src/clients/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,17 @@ impl Clients {
}

#[cfg(feature = "upower")]
pub fn upower(&mut self) -> Arc<zbus::fdo::PropertiesProxy<'static>> {
self.upower
.get_or_insert_with(|| {
crate::await_sync(async { upower::create_display_proxy().await })
})
.clone()
pub fn upower(&mut self) -> ClientResult<zbus::fdo::PropertiesProxy<'static>> {
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")]
Expand Down
25 changes: 9 additions & 16 deletions src/clients/upower.rs
Original file line number Diff line number Diff line change
@@ -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<PropertiesProxy<'static>> {
let dbus = Box::pin(zbus::Connection::system())
.await
.expect("failed to create connection to system bus");
pub async fn create_display_proxy() -> ClientResult<PropertiesProxy<'static>> {
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();

Expand All @@ -26,10 +20,9 @@ pub async fn create_display_proxy() -> Arc<PropertiesProxy<'static>> {
.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);
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/upower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Module<gtk::Button> for UpowerModule {
) -> Result<()> {
let tx = context.tx.clone();

let display_proxy = context.client::<PropertiesProxy>();
let display_proxy = context.try_client::<PropertiesProxy>()?;

spawn(async move {
let mut prop_changed_stream = display_proxy.receive_properties_changed().await?;
Expand Down

0 comments on commit 474e1fe

Please sign in to comment.