Skip to content

Commit

Permalink
Improvements to error handling
Browse files Browse the repository at this point in the history
- Add `log` dependency.
- Remove `anyhow` dependency in favor of standard `Result`.
- Handle `unwrap()` calls and report errors with `log::error!`.
  • Loading branch information
edfloreshz committed Oct 8, 2024
1 parent 925fe9d commit 08f0648
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 3 additions & 1 deletion examples/notify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::error::Error;

use futures::StreamExt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> Result<(), Box<dyn Error>> {
let mut stream = dark_light::subscribe().await?;
while let Some(mode) = stream.next().await {
println!("System theme changed: {:?}", mode);
Expand Down
3 changes: 1 addition & 2 deletions src/platforms/freedesktop/detect.rs
Original file line number Diff line number Diff line change
@@ -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())
}
20 changes: 12 additions & 8 deletions src/platforms/freedesktop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ impl From<PortalColorScheme> 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()
}
6 changes: 4 additions & 2 deletions src/platforms/freedesktop/notify.rs
Original file line number Diff line number Diff line change
@@ -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<impl Stream<Item = Mode> + Send> {
let initial = stream::once(super::initial_value()).boxed();
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let initial = stream::once(super::get_color_scheme()).boxed();
let later_updates = Settings::new()
.await?
.receive_color_scheme_changed()
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/macos/notify.rs
Original file line number Diff line number Diff line change
@@ -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<impl Stream<Item = Mode> + Send> {
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let mut last_mode = detect();

let stream = stream::poll_fn(move |ctx| -> Poll<Option<Mode>> {
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/websys/notify.rs
Original file line number Diff line number Diff line change
@@ -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<impl Stream<Item = Mode> + Send> {
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let mut last_mode = detect();

let stream = stream::poll_fn(move |ctx| -> Poll<Option<Mode>> {
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/windows/notify.rs
Original file line number Diff line number Diff line change
@@ -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<impl Stream<Item = Mode> + Send> {
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let mut last_mode = detect();

let stream = stream::poll_fn(move |ctx| -> Poll<Option<Mode>> {
Expand Down

0 comments on commit 08f0648

Please sign in to comment.