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..789793f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! # Examples //! //! ``` -//! # fn try_main() -> anyhow::Result<()> { +//! # fn try_main() -> Result<(), keepawake::Error> { //! let _awake = keepawake::Builder::default() //! .display(true) //! .reason("Video playback") @@ -16,7 +16,7 @@ //! ``` //! //! ``` -//! # fn try_main() -> anyhow::Result<()> { +//! # fn try_main() -> Result<(), keepawake::Error> { //! let _awake = keepawake::Builder::default() //! .display(true) //! .idle(true) @@ -27,14 +27,25 @@ //! # try_main(); //! ``` -use anyhow::Result; +use std::error; + use derive_builder::Builder; +use thiserror::Error; mod sys; #[cfg(feature = "capi")] pub mod capi; +#[derive(Error, Debug)] +pub enum Error { + #[error("builder: {0}")] + Builder(#[from] BuilderError), + + #[error("system: {0}")] + System(#[from] Box), +} + #[derive(Builder, Debug)] #[builder(public, name = "Builder", build_fn(private))] #[allow(dead_code)] // Some fields are unused on some platforms @@ -70,7 +81,7 @@ struct Options { impl Builder { /// Create the [`KeepAwake`]. - pub fn create(&self) -> Result { + pub fn create(&self) -> Result { Ok(KeepAwake { _imp: sys::KeepAwake::new(self.build()?)?, }) diff --git a/src/sys/linux.rs b/src/sys/linux.rs index ecff3ba..1856860 100644 --- a/src/sys/linux.rs +++ b/src/sys/linux.rs @@ -7,7 +7,8 @@ //! [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 std::error; + use zbus::{blocking::Connection, dbus_proxy}; use crate::Options; @@ -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..1ed4120 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, @@ -31,7 +32,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 +43,7 @@ impl KeepAwake { Ok(awake) } - fn set(&mut self) -> Result<()> { + fn set(&mut self) -> Result<(), Box> { if self.options.display { unsafe { let result = IOPMAssertionCreateWithName( @@ -55,7 +56,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 +71,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 +86,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..c12e59c 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -7,7 +7,8 @@ //! [`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 std::error; + use windows::core::Error as WindowsError; use windows::Win32::System::Power::{ SetThreadExecutionState, ES_AWAYMODE_REQUIRED, ES_CONTINUOUS, ES_DISPLAY_REQUIRED, @@ -22,7 +23,7 @@ pub struct KeepAwake { } impl KeepAwake { - pub fn new(options: Options) -> Result { + pub fn new(options: Options) -> Result> { let mut awake = KeepAwake { options, previous: Default::default(),