Skip to content

Commit

Permalink
remove: conditional errors
Browse files Browse the repository at this point in the history
  • Loading branch information
edfloreshz authored and Be-ing committed Jan 9, 2025
1 parent 15ebb2f commit b10982b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 58 deletions.
57 changes: 1 addition & 56 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,18 @@ pub enum Error {
Io(std::io::Error),
/// If the system theme mode could not be detected.
DetectionFailed,

#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
/// If the XDG Desktop Portal could not be communicated with.
XdgDesktopPortal(ashpd::Error),

#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
XdgDesktopPortal(String),
/// If the timeout is reached.
Timeout,

/// Failed to get persistent domain for Apple Global Domain
#[cfg(target_os = "macos")]
PersistentDomainFailed,
/// Failed to get AppleInterfaceStyle
#[cfg(target_os = "macos")]
AppleInterfaceStyleFailed,

#[cfg(target_arch = "wasm32")]
/// If the window could not be found.
WindowNotFound,
#[cfg(target_arch = "wasm32")]
/// If the media query could not be executed.
MediaQueryFailed,
#[cfg(target_arch = "wasm32")]
/// If the media query is not supported.
MediaQueryNotSupported,
}
Expand All @@ -51,33 +28,14 @@ impl Display for Error {
match self {
Error::Io(error) => write!(f, "I/O error: {}", error),
Error::DetectionFailed => write!(f, "Failed to detect system theme mode"),
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
Error::XdgDesktopPortal(err) => write!(f, "XDG Desktop Portal error: {}", err),
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
Error::Timeout => write!(f, "Timeout reached"),
#[cfg(target_os = "macos")]
Error::PersistentDomainFailed => {
write!(f, "Failed to get persistent domain for Apple Global Domain")
}
#[cfg(target_os = "macos")]
Error::AppleInterfaceStyleFailed => write!(f, "Failed to get AppleInterfaceStyle"),
#[cfg(target_arch = "wasm32")]
Error::WindowNotFound => write!(f, "Window not found"),
#[cfg(target_arch = "wasm32")]
Error::MediaQueryFailed => write!(f, "Media query failed"),
#[cfg(target_arch = "wasm32")]
Error::MediaQueryNotSupported => write!(f, "Media query not supported"),
}
}
Expand All @@ -90,16 +48,3 @@ impl From<std::io::Error> for Error {
Error::Io(error)
}
}

#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
impl From<ashpd::Error> for Error {
fn from(error: ashpd::Error) -> Self {
Error::XdgDesktopPortal(error)
}
}
9 changes: 7 additions & 2 deletions src/platforms/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ use async_std::{future, task};

pub fn detect() -> Result<Mode, Error> {
task::block_on(future::timeout(Duration::from_millis(25), async {
let settings = XdgPortalSettings::new().await?;
let color_scheme = settings.color_scheme().await?;
let settings = XdgPortalSettings::new()
.await
.map_err(|e| Error::XdgDesktopPortal(e.to_string()))?;
let color_scheme = settings
.color_scheme()
.await
.map_err(|e| Error::XdgDesktopPortal(e.to_string()))?;
Ok::<Mode, Error>(color_scheme.into())
}))
.map_err(|_| Error::Timeout)?
Expand Down

0 comments on commit b10982b

Please sign in to comment.