Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try switching to thiserror for the library part (Variant #2) #25

Merged
merged 11 commits into from
Feb 11, 2024
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -24,14 +32,15 @@ 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 }
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"
Expand Down
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -16,7 +16,7 @@
//! ```
//!
//! ```
//! # fn try_main() -> anyhow::Result<()> {
//! # fn try_main() -> keepawake::Result<()> {
//! let _awake = keepawake::Builder::default()
//! .display(true)
//! .idle(true)
Expand All @@ -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<T, E = Error> = std::result::Result<T, E>;

#[derive(Builder, Debug)]
#[builder(public, name = "Builder", build_fn(private))]
#[allow(dead_code)] // Some fields are unused on some platforms
Expand Down
5 changes: 3 additions & 2 deletions src/sys/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -51,7 +52,7 @@ pub struct KeepAwake {
}

impl KeepAwake {
pub fn new(options: Options) -> Result<Self> {
pub fn new(options: Options) -> Result<Self, zbus::Error> {
let mut awake = Self {
options,

Expand Down
15 changes: 9 additions & 6 deletions src/sys/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,6 +14,8 @@ use core_foundation::{base::TCFType, string::CFString};

use crate::Options;

pub type Error = Box<dyn error::Error + Send + Sync>;

#[allow(non_upper_case_globals)]
const kIOPMAssertionTypePreventUserIdleSystemSleep: &str = "PreventUserIdleSystemSleep";

Expand All @@ -31,7 +34,7 @@ pub struct KeepAwake {
}

impl KeepAwake {
pub fn new(options: Options) -> Result<Self> {
pub fn new(options: Options) -> Result<Self, Error> {
let mut awake = Self {
options,
display_assertion: 0,
Expand All @@ -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(
Expand All @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Self> {
pub fn new(options: Options) -> Result<Self, Error> {
let mut awake = KeepAwake {
options,
previous: Default::default(),
Expand All @@ -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 {
Expand Down
Loading