diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index affb1d9..105470d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,6 @@ on: push: branches: [ "main" ] pull_request: - branches: [ "main" ] env: CARGO_TERM_COLOR: always diff --git a/Cargo.lock b/Cargo.lock index 0821fb6..03f2683 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1063,6 +1063,7 @@ dependencies = [ "derive_builder", "shadow-rs", "sysinfo", + "thiserror", "windows", "winresource", "zbus", diff --git a/Cargo.toml b/Cargo.toml index a1c3af7..e118af4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,15 @@ categories = ["command-line-utilities"] exclude = ["/tools/"] [features] -bin = ["dep:clap", "dep:clap_complete", "dep:ctrlc", "dep:shadow-rs", "dep:sysinfo", "dep:winresource"] +bin = [ + "dep:anyhow", + "dep:clap", + "dep:clap_complete", + "dep:ctrlc", + "dep:shadow-rs", + "dep:sysinfo", + "dep:winresource" +] capi = [] [profile.release] @@ -24,7 +32,7 @@ name = "keepawake" required-features = ["bin"] [dependencies] -anyhow = "1.0.65" +anyhow = { version = "1.0.65", optional = true } cfg-if = "1.0.0" clap = { version = "4.0.2", features = ["derive"], optional = true } clap_complete = { version = "4.0.2", optional = true } @@ -32,6 +40,7 @@ ctrlc = { version = "3.2.3", features = ["termination"], optional = true } derive_builder = "0.12.0" shadow-rs = { version = "0.26.1", optional = true } sysinfo = { version = "0.30.5", optional = true } +thiserror = "1.0.56" [target.'cfg(windows)'.dependencies.windows] version = "0.52.0" diff --git a/src/lib.rs b/src/lib.rs index f627c72..0a05d76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! # Examples //! //! ``` -//! # fn try_main() -> anyhow::Result<()> { +//! # fn try_main() -> keepawake::Result<()> { //! let _awake = keepawake::Builder::default() //! .display(true) //! .reason("Video playback") @@ -16,7 +16,7 @@ //! ``` //! //! ``` -//! # fn try_main() -> anyhow::Result<()> { +//! # fn try_main() -> keepawake::Result<()> { //! let _awake = keepawake::Builder::default() //! .display(true) //! .idle(true) @@ -27,14 +27,30 @@ //! # try_main(); //! ``` -use anyhow::Result; use derive_builder::Builder; +use thiserror::Error; mod sys; #[cfg(feature = "capi")] pub mod capi; +/// A system error whose actual type varies by target. +pub use sys::Error as SystemError; + +/// Error type. +#[derive(Error, Debug)] +pub enum Error { + #[error("builder: {0}")] + Builder(#[from] BuilderError), + + #[error("system: {0}")] + System(#[from] SystemError), +} + +/// A specialized [`Result`](std::result::Result) type for this crate. +pub type Result = std::result::Result; + #[derive(Builder, Debug)] #[builder(public, name = "Builder", build_fn(private))] #[allow(dead_code)] // Some fields are unused on some platforms diff --git a/src/sys/linux.rs b/src/sys/linux.rs index ecff3ba..ecdcbb2 100644 --- a/src/sys/linux.rs +++ b/src/sys/linux.rs @@ -7,11 +7,12 @@ //! [ScreenSaver]: https://people.freedesktop.org/~hadess/idle-inhibition-spec/re01.html //! [systemd Inhibitor Locks]:(https://www.freedesktop.org/wiki/Software/systemd/inhibit/ -use anyhow::Result; use zbus::{blocking::Connection, dbus_proxy}; use crate::Options; +pub type Error = zbus::Error; + #[dbus_proxy( interface = "org.freedesktop.login1.Manager", default_service = "org.freedesktop.login1", @@ -51,7 +52,7 @@ pub struct KeepAwake { } impl KeepAwake { - pub fn new(options: Options) -> Result { + pub fn new(options: Options) -> Result { let mut awake = Self { options, diff --git a/src/sys/macos.rs b/src/sys/macos.rs index 420b0a4..0582552 100644 --- a/src/sys/macos.rs +++ b/src/sys/macos.rs @@ -4,7 +4,8 @@ //! //! [`IOPMAssertionCreateWithName`]: https://developer.apple.com/documentation/iokit/1557134-iopmassertioncreatewithname -use anyhow::{anyhow, Result}; +use std::error; + use apple_sys::IOKit::{ kIOPMAssertionLevelOn, kIOReturnSuccess, CFStringRef, IOPMAssertionCreateWithName, IOPMAssertionRelease, @@ -13,6 +14,8 @@ use core_foundation::{base::TCFType, string::CFString}; use crate::Options; +pub type Error = Box; + #[allow(non_upper_case_globals)] const kIOPMAssertionTypePreventUserIdleSystemSleep: &str = "PreventUserIdleSystemSleep"; @@ -31,7 +34,7 @@ pub struct KeepAwake { } impl KeepAwake { - pub fn new(options: Options) -> Result { + pub fn new(options: Options) -> Result { let mut awake = Self { options, display_assertion: 0, @@ -42,7 +45,7 @@ impl KeepAwake { Ok(awake) } - fn set(&mut self) -> Result<()> { + fn set(&mut self) -> Result<(), Error> { if self.options.display { unsafe { let result = IOPMAssertionCreateWithName( @@ -55,7 +58,7 @@ impl KeepAwake { ); if result != kIOReturnSuccess as i32 { // TODO Better error? - return Err(anyhow!("IO error: {:#x}", result)); + return Err(format!("IO error: {:#x}", result).into()); } } } @@ -70,7 +73,7 @@ impl KeepAwake { &mut self.idle_assertion, ); if result != kIOReturnSuccess as i32 { - return Err(anyhow!("IO error: {:#x}", result)); + return Err(format!("IO error: {:#x}", result).into()); } } } @@ -85,7 +88,7 @@ impl KeepAwake { &mut self.sleep_assertion, ); if result != kIOReturnSuccess as i32 { - return Err(anyhow!("IO error: {:#x}", result)); + return Err(format!("IO error: {:#x}", result).into()); } } } diff --git a/src/sys/windows.rs b/src/sys/windows.rs index b9fca90..adf46b7 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -7,7 +7,6 @@ //! [`SetThreadExecutionState`]: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate //! [`PowerSetRequest`]: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-powersetrequest -use anyhow::Result; use windows::core::Error as WindowsError; use windows::Win32::System::Power::{ SetThreadExecutionState, ES_AWAYMODE_REQUIRED, ES_CONTINUOUS, ES_DISPLAY_REQUIRED, @@ -16,13 +15,15 @@ use windows::Win32::System::Power::{ use crate::Options; +pub type Error = WindowsError; + pub struct KeepAwake { options: Options, previous: EXECUTION_STATE, } impl KeepAwake { - pub fn new(options: Options) -> Result { + pub fn new(options: Options) -> Result { let mut awake = KeepAwake { options, previous: Default::default(), @@ -31,7 +32,7 @@ impl KeepAwake { Ok(awake) } - fn set(&mut self) -> Result<(), WindowsError> { + fn set(&mut self) -> Result<(), Error> { let mut esflags = ES_CONTINUOUS; if self.options.display {