From 38dc32a6124f1871b878f9e00308826482965554 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Fri, 19 Jan 2024 14:21:18 +0200 Subject: [PATCH 01/10] Try switching to thiserror for the library --- Cargo.lock | 1 + Cargo.toml | 1 + src/lib.rs | 13 +++++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 61d8450..203362f 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 ca9817f..04283b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,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 c8b8349..b65da30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,11 +27,20 @@ //! # try_main(); //! ``` -use anyhow::Result; use derive_builder::Builder; +use thiserror::Error; mod sys; +#[derive(Error, Debug)] +pub enum Error { + #[error(transparent)] + Builder(#[from] BuilderError), + + #[error("system: {0}")] + System(#[from] anyhow::Error), +} + #[derive(Builder, Debug)] #[builder(public, name = "Builder", build_fn(private))] #[allow(dead_code)] // Some fields are unused on some platforms @@ -66,7 +75,7 @@ struct Options { } impl Builder { - pub fn create(&self) -> Result { + pub fn create(&self) -> Result { Ok(KeepAwake { _imp: sys::KeepAwake::new(self.build()?)?, }) From 7798c09e66f5be86e6ff0a19923b93aea53a38ef Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Fri, 19 Jan 2024 14:37:54 +0200 Subject: [PATCH 02/10] Try switching to thiserror #2 --- Cargo.toml | 12 ++++++++++-- src/lib.rs | 4 +++- src/sys/linux.rs | 5 +++-- src/sys/macos.rs | 9 ++++----- src/sys/windows.rs | 5 +++-- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 04283b2..3b5724c 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" +] [profile.release] strip = true @@ -23,7 +31,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 } diff --git a/src/lib.rs b/src/lib.rs index b65da30..a40a998 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,8 @@ //! # try_main(); //! ``` +use std::error; + use derive_builder::Builder; use thiserror::Error; @@ -38,7 +40,7 @@ pub enum Error { Builder(#[from] BuilderError), #[error("system: {0}")] - System(#[from] anyhow::Error), + System(#[from] Box), } #[derive(Builder, Debug)] 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..82ab8c2 100644 --- a/src/sys/macos.rs +++ b/src/sys/macos.rs @@ -4,7 +4,6 @@ //! //! [`IOPMAssertionCreateWithName`]: https://developer.apple.com/documentation/iokit/1557134-iopmassertioncreatewithname -use anyhow::{anyhow, Result}; use apple_sys::IOKit::{ kIOPMAssertionLevelOn, kIOReturnSuccess, CFStringRef, IOPMAssertionCreateWithName, IOPMAssertionRelease, @@ -42,7 +41,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 +54,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 +69,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 +84,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(), From 5abc4b1075056851d7982b6dccd4ea607ff2a1be Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Fri, 19 Jan 2024 14:49:36 +0200 Subject: [PATCH 03/10] Missing import --- src/sys/macos.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sys/macos.rs b/src/sys/macos.rs index 82ab8c2..394053e 100644 --- a/src/sys/macos.rs +++ b/src/sys/macos.rs @@ -4,6 +4,8 @@ //! //! [`IOPMAssertionCreateWithName`]: https://developer.apple.com/documentation/iokit/1557134-iopmassertioncreatewithname +use std::error; + use apple_sys::IOKit::{ kIOPMAssertionLevelOn, kIOReturnSuccess, CFStringRef, IOPMAssertionCreateWithName, IOPMAssertionRelease, From d2b39f145b3c75acff3a5c7e0045e68dc088d815 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Sun, 21 Jan 2024 00:44:42 +0200 Subject: [PATCH 04/10] Fix --- .github/workflows/ci.yml | 1 - src/sys/macos.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) 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/src/sys/macos.rs b/src/sys/macos.rs index 394053e..1ed4120 100644 --- a/src/sys/macos.rs +++ b/src/sys/macos.rs @@ -32,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, From 74e1fd8cff7b215fbd8606212b863560f9fa8c5c Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 6 Feb 2024 01:06:19 +0200 Subject: [PATCH 05/10] Fix doc tests --- src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1413ac9..cc6623d 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,8 +27,6 @@ //! # try_main(); //! ``` -use std::error; - use derive_builder::Builder; use thiserror::Error; From d60ad938231786a9e77147343c29d5c3ffe3d826 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 6 Feb 2024 01:13:08 +0200 Subject: [PATCH 06/10] Modify Builder error variant to not be transparent --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cc6623d..75f0a4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ pub mod capi; #[derive(Error, Debug)] pub enum Error { - #[error(transparent)] + #[error("builder: {0}")] Builder(#[from] BuilderError), #[error("system: {0}")] From e23cf3ae7b1b75b49ae6682ffb007b98b15830cd Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 6 Feb 2024 01:13:38 +0200 Subject: [PATCH 07/10] Use sys::Error alias for the system variant --- src/lib.rs | 2 +- src/sys/linux.rs | 4 +++- src/sys/macos.rs | 6 ++++-- src/sys/windows.rs | 6 ++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 75f0a4b..668d06b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ pub enum Error { Builder(#[from] BuilderError), #[error("system: {0}")] - System(#[from] Box), + System(#[from] sys::Error), } #[derive(Builder, Debug)] diff --git a/src/sys/linux.rs b/src/sys/linux.rs index 1856860..c9b41ed 100644 --- a/src/sys/linux.rs +++ b/src/sys/linux.rs @@ -13,6 +13,8 @@ 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", @@ -52,7 +54,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 1ed4120..0582552 100644 --- a/src/sys/macos.rs +++ b/src/sys/macos.rs @@ -14,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"; @@ -32,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, @@ -43,7 +45,7 @@ impl KeepAwake { Ok(awake) } - fn set(&mut self) -> Result<(), Box> { + fn set(&mut self) -> Result<(), Error> { if self.options.display { unsafe { let result = IOPMAssertionCreateWithName( diff --git a/src/sys/windows.rs b/src/sys/windows.rs index c12e59c..37e2387 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -17,13 +17,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(), @@ -32,7 +34,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 { From 245bb780b81126e6a9f5f288c20465bf6a69928c Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 6 Feb 2024 01:15:49 +0200 Subject: [PATCH 08/10] Remove unused imports --- src/sys/linux.rs | 2 -- src/sys/windows.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/sys/linux.rs b/src/sys/linux.rs index c9b41ed..ecdcbb2 100644 --- a/src/sys/linux.rs +++ b/src/sys/linux.rs @@ -7,8 +7,6 @@ //! [ScreenSaver]: https://people.freedesktop.org/~hadess/idle-inhibition-spec/re01.html //! [systemd Inhibitor Locks]:(https://www.freedesktop.org/wiki/Software/systemd/inhibit/ -use std::error; - use zbus::{blocking::Connection, dbus_proxy}; use crate::Options; diff --git a/src/sys/windows.rs b/src/sys/windows.rs index 37e2387..adf46b7 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -7,8 +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 std::error; - use windows::core::Error as WindowsError; use windows::Win32::System::Power::{ SetThreadExecutionState, ES_AWAYMODE_REQUIRED, ES_CONTINUOUS, ES_DISPLAY_REQUIRED, From b22631df8c369de5b9772e888242eebd272a983f Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 6 Feb 2024 01:17:58 +0200 Subject: [PATCH 09/10] Format --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 668d06b..05c4460 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,6 @@ use thiserror::Error; mod sys; - #[cfg(feature = "capi")] pub mod capi; From 0d3a37eed18d46c10d156a104cf0900b07c9bc4c Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Wed, 7 Feb 2024 15:24:12 +0200 Subject: [PATCH 10/10] Further error type tweaks --- src/lib.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 05c4460..0a05d76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! # Examples //! //! ``` -//! # fn try_main() -> Result<(), keepawake::Error> { +//! # fn try_main() -> keepawake::Result<()> { //! let _awake = keepawake::Builder::default() //! .display(true) //! .reason("Video playback") @@ -16,7 +16,7 @@ //! ``` //! //! ``` -//! # fn try_main() -> Result<(), keepawake::Error> { +//! # fn try_main() -> keepawake::Result<()> { //! let _awake = keepawake::Builder::default() //! .display(true) //! .idle(true) @@ -35,15 +35,22 @@ 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] sys::Error), + 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 @@ -79,7 +86,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()?)?, })