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 #22

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 15 additions & 4 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() -> Result<(), keepawake::Error> {
//! let _awake = keepawake::Builder::default()
//! .display(true)
//! .reason("Video playback")
Expand All @@ -16,7 +16,7 @@
//! ```
//!
//! ```
//! # fn try_main() -> anyhow::Result<()> {
//! # fn try_main() -> Result<(), keepawake::Error> {
//! let _awake = keepawake::Builder::default()
//! .display(true)
//! .idle(true)
Expand All @@ -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<dyn error::Error + Send + Sync>),
}

#[derive(Builder, Debug)]
#[builder(public, name = "Builder", build_fn(private))]
#[allow(dead_code)] // Some fields are unused on some platforms
Expand Down Expand Up @@ -70,7 +81,7 @@ struct Options {

impl Builder {
/// Create the [`KeepAwake`].
pub fn create(&self) -> Result<KeepAwake> {
pub fn create(&self) -> Result<KeepAwake, Error> {
Ok(KeepAwake {
_imp: sys::KeepAwake::new(self.build()?)?,
})
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,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;
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, Box<dyn error::Error + Send + Sync>> {
let mut awake = Self {
options,

Expand Down
13 changes: 7 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 @@ -31,7 +32,7 @@ pub struct KeepAwake {
}

impl KeepAwake {
pub fn new(options: Options) -> Result<Self> {
pub fn new(options: Options) -> Result<Self, Box<dyn error::Error + Send + Sync>> {
let mut awake = Self {
options,
display_assertion: 0,
Expand All @@ -42,7 +43,7 @@ impl KeepAwake {
Ok(awake)
}

fn set(&mut self) -> Result<()> {
fn set(&mut self) -> Result<(), Box<dyn error::Error + Send + Sync>> {
if self.options.display {
unsafe {
let result = IOPMAssertionCreateWithName(
Expand All @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,7 +23,7 @@ pub struct KeepAwake {
}

impl KeepAwake {
pub fn new(options: Options) -> Result<Self> {
pub fn new(options: Options) -> Result<Self, Box<dyn error::Error + Send + Sync>> {
let mut awake = KeepAwake {
options,
previous: Default::default(),
Expand Down
Loading