From 08f06481a689b8159095ba84278c548615197089 Mon Sep 17 00:00:00 2001 From: Eduardo Flores Date: Tue, 8 Oct 2024 21:26:01 +0200 Subject: [PATCH] Improvements to error handling - Add `log` dependency. - Remove `anyhow` dependency in favor of standard `Result`. - Handle `unwrap()` calls and report errors with `log::error!`. --- Cargo.toml | 2 +- examples/notify.rs | 4 +++- src/platforms/freedesktop/detect.rs | 3 +-- src/platforms/freedesktop/mod.rs | 20 ++++++++++++-------- src/platforms/freedesktop/notify.rs | 6 ++++-- src/platforms/macos/notify.rs | 3 ++- src/platforms/websys/notify.rs | 3 ++- src/platforms/windows/notify.rs | 3 ++- 8 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9a4be90..6f74b94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ build = "build.rs" [dependencies] futures = "0.3.30" -anyhow = "1.0.79" +log = "0.4.22" [dev-dependencies] tokio = { version = "1.23.0", features = ["full"] } diff --git a/examples/notify.rs b/examples/notify.rs index 6ade504..fcf8bb5 100644 --- a/examples/notify.rs +++ b/examples/notify.rs @@ -1,7 +1,9 @@ +use std::error::Error; + use futures::StreamExt; #[tokio::main] -async fn main() -> anyhow::Result<()> { +async fn main() -> Result<(), Box> { let mut stream = dark_light::subscribe().await?; while let Some(mode) = stream.next().await { println!("System theme changed: {:?}", mode); diff --git a/src/platforms/freedesktop/detect.rs b/src/platforms/freedesktop/detect.rs index 0afd337..8fea278 100644 --- a/src/platforms/freedesktop/detect.rs +++ b/src/platforms/freedesktop/detect.rs @@ -1,6 +1,5 @@ -use super::initial_value; use crate::Mode; pub fn detect() -> Mode { - pollster::block_on(initial_value()) + pollster::block_on(super::get_color_scheme()) } diff --git a/src/platforms/freedesktop/mod.rs b/src/platforms/freedesktop/mod.rs index 59bad5d..2731356 100644 --- a/src/platforms/freedesktop/mod.rs +++ b/src/platforms/freedesktop/mod.rs @@ -16,12 +16,16 @@ impl From for Mode { } } -pub(crate) async fn initial_value() -> Mode { - XdgPortalSettings::new() - .await - .unwrap() - .color_scheme() - .await - .unwrap() - .into() +pub(crate) async fn get_color_scheme() -> Mode { + let Ok(settings) = XdgPortalSettings::new().await else { + log::error!("Failed to create a new portal settings instance."); + return Mode::Default; + }; + + let Ok(color_scheme) = settings.color_scheme().await else { + log::error!("Failed to get the current color scheme, defaulting to Mode::Default."); + return Mode::Default; + }; + + color_scheme.into() } diff --git a/src/platforms/freedesktop/notify.rs b/src/platforms/freedesktop/notify.rs index 74c2a98..935e748 100644 --- a/src/platforms/freedesktop/notify.rs +++ b/src/platforms/freedesktop/notify.rs @@ -1,10 +1,12 @@ +use std::error::Error; + use ashpd::desktop::settings::Settings; use futures::{stream, Stream, StreamExt}; use crate::Mode; -pub async fn subscribe() -> anyhow::Result + Send> { - let initial = stream::once(super::initial_value()).boxed(); +pub async fn subscribe() -> Result + Send, Box> { + let initial = stream::once(super::get_color_scheme()).boxed(); let later_updates = Settings::new() .await? .receive_color_scheme_changed() diff --git a/src/platforms/macos/notify.rs b/src/platforms/macos/notify.rs index 305205f..8c3608c 100644 --- a/src/platforms/macos/notify.rs +++ b/src/platforms/macos/notify.rs @@ -1,10 +1,11 @@ +use std::error::Error; use std::task::Poll; use futures::{stream, Stream}; use crate::{detect, Mode}; -pub async fn subscribe() -> anyhow::Result + Send> { +pub async fn subscribe() -> Result + Send, Box> { let mut last_mode = detect(); let stream = stream::poll_fn(move |ctx| -> Poll> { diff --git a/src/platforms/websys/notify.rs b/src/platforms/websys/notify.rs index 305205f..8c3608c 100644 --- a/src/platforms/websys/notify.rs +++ b/src/platforms/websys/notify.rs @@ -1,10 +1,11 @@ +use std::error::Error; use std::task::Poll; use futures::{stream, Stream}; use crate::{detect, Mode}; -pub async fn subscribe() -> anyhow::Result + Send> { +pub async fn subscribe() -> Result + Send, Box> { let mut last_mode = detect(); let stream = stream::poll_fn(move |ctx| -> Poll> { diff --git a/src/platforms/windows/notify.rs b/src/platforms/windows/notify.rs index 22a91fd..7a3621a 100644 --- a/src/platforms/windows/notify.rs +++ b/src/platforms/windows/notify.rs @@ -1,10 +1,11 @@ +use std::error::Error; use std::task::Poll; use futures::{stream, Stream}; use crate::{detect, Mode}; -pub async fn subscribe() -> anyhow::Result + Send> { +pub async fn subscribe() -> Result + Send, Box> { let mut last_mode = detect(); let stream = stream::poll_fn(move |ctx| -> Poll> {