Skip to content

Commit 19aadfe

Browse files
authored
Merge pull request #25 from segevfiner/thiserror-2
Try switching to thiserror for the library part (Variant #2)
2 parents f588ebc + 0d3a37e commit 19aadfe

File tree

7 files changed

+47
-17
lines changed

7 files changed

+47
-17
lines changed

.github/workflows/ci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ on:
44
push:
55
branches: [ "main" ]
66
pull_request:
7-
branches: [ "main" ]
87

98
env:
109
CARGO_TERM_COLOR: always

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ categories = ["command-line-utilities"]
1212
exclude = ["/tools/"]
1313

1414
[features]
15-
bin = ["dep:clap", "dep:clap_complete", "dep:ctrlc", "dep:shadow-rs", "dep:sysinfo", "dep:winresource"]
15+
bin = [
16+
"dep:anyhow",
17+
"dep:clap",
18+
"dep:clap_complete",
19+
"dep:ctrlc",
20+
"dep:shadow-rs",
21+
"dep:sysinfo",
22+
"dep:winresource"
23+
]
1624
capi = []
1725

1826
[profile.release]
@@ -24,14 +32,15 @@ name = "keepawake"
2432
required-features = ["bin"]
2533

2634
[dependencies]
27-
anyhow = "1.0.65"
35+
anyhow = { version = "1.0.65", optional = true }
2836
cfg-if = "1.0.0"
2937
clap = { version = "4.0.2", features = ["derive"], optional = true }
3038
clap_complete = { version = "4.0.2", optional = true }
3139
ctrlc = { version = "3.2.3", features = ["termination"], optional = true }
3240
derive_builder = "0.12.0"
3341
shadow-rs = { version = "0.26.1", optional = true }
3442
sysinfo = { version = "0.30.5", optional = true }
43+
thiserror = "1.0.56"
3544

3645
[target.'cfg(windows)'.dependencies.windows]
3746
version = "0.52.0"

src/lib.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! # Examples
44
//!
55
//! ```
6-
//! # fn try_main() -> anyhow::Result<()> {
6+
//! # fn try_main() -> keepawake::Result<()> {
77
//! let _awake = keepawake::Builder::default()
88
//! .display(true)
99
//! .reason("Video playback")
@@ -16,7 +16,7 @@
1616
//! ```
1717
//!
1818
//! ```
19-
//! # fn try_main() -> anyhow::Result<()> {
19+
//! # fn try_main() -> keepawake::Result<()> {
2020
//! let _awake = keepawake::Builder::default()
2121
//! .display(true)
2222
//! .idle(true)
@@ -27,14 +27,30 @@
2727
//! # try_main();
2828
//! ```
2929
30-
use anyhow::Result;
3130
use derive_builder::Builder;
31+
use thiserror::Error;
3232

3333
mod sys;
3434

3535
#[cfg(feature = "capi")]
3636
pub mod capi;
3737

38+
/// A system error whose actual type varies by target.
39+
pub use sys::Error as SystemError;
40+
41+
/// Error type.
42+
#[derive(Error, Debug)]
43+
pub enum Error {
44+
#[error("builder: {0}")]
45+
Builder(#[from] BuilderError),
46+
47+
#[error("system: {0}")]
48+
System(#[from] SystemError),
49+
}
50+
51+
/// A specialized [`Result`](std::result::Result) type for this crate.
52+
pub type Result<T, E = Error> = std::result::Result<T, E>;
53+
3854
#[derive(Builder, Debug)]
3955
#[builder(public, name = "Builder", build_fn(private))]
4056
#[allow(dead_code)] // Some fields are unused on some platforms

src/sys/linux.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
//! [ScreenSaver]: https://people.freedesktop.org/~hadess/idle-inhibition-spec/re01.html
88
//! [systemd Inhibitor Locks]:(https://www.freedesktop.org/wiki/Software/systemd/inhibit/
99
10-
use anyhow::Result;
1110
use zbus::{blocking::Connection, dbus_proxy};
1211

1312
use crate::Options;
1413

14+
pub type Error = zbus::Error;
15+
1516
#[dbus_proxy(
1617
interface = "org.freedesktop.login1.Manager",
1718
default_service = "org.freedesktop.login1",
@@ -51,7 +52,7 @@ pub struct KeepAwake {
5152
}
5253

5354
impl KeepAwake {
54-
pub fn new(options: Options) -> Result<Self> {
55+
pub fn new(options: Options) -> Result<Self, zbus::Error> {
5556
let mut awake = Self {
5657
options,
5758

src/sys/macos.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//!
55
//! [`IOPMAssertionCreateWithName`]: https://developer.apple.com/documentation/iokit/1557134-iopmassertioncreatewithname
66
7-
use anyhow::{anyhow, Result};
7+
use std::error;
8+
89
use apple_sys::IOKit::{
910
kIOPMAssertionLevelOn, kIOReturnSuccess, CFStringRef, IOPMAssertionCreateWithName,
1011
IOPMAssertionRelease,
@@ -13,6 +14,8 @@ use core_foundation::{base::TCFType, string::CFString};
1314

1415
use crate::Options;
1516

17+
pub type Error = Box<dyn error::Error + Send + Sync>;
18+
1619
#[allow(non_upper_case_globals)]
1720
const kIOPMAssertionTypePreventUserIdleSystemSleep: &str = "PreventUserIdleSystemSleep";
1821

@@ -31,7 +34,7 @@ pub struct KeepAwake {
3134
}
3235

3336
impl KeepAwake {
34-
pub fn new(options: Options) -> Result<Self> {
37+
pub fn new(options: Options) -> Result<Self, Error> {
3538
let mut awake = Self {
3639
options,
3740
display_assertion: 0,
@@ -42,7 +45,7 @@ impl KeepAwake {
4245
Ok(awake)
4346
}
4447

45-
fn set(&mut self) -> Result<()> {
48+
fn set(&mut self) -> Result<(), Error> {
4649
if self.options.display {
4750
unsafe {
4851
let result = IOPMAssertionCreateWithName(
@@ -55,7 +58,7 @@ impl KeepAwake {
5558
);
5659
if result != kIOReturnSuccess as i32 {
5760
// TODO Better error?
58-
return Err(anyhow!("IO error: {:#x}", result));
61+
return Err(format!("IO error: {:#x}", result).into());
5962
}
6063
}
6164
}
@@ -70,7 +73,7 @@ impl KeepAwake {
7073
&mut self.idle_assertion,
7174
);
7275
if result != kIOReturnSuccess as i32 {
73-
return Err(anyhow!("IO error: {:#x}", result));
76+
return Err(format!("IO error: {:#x}", result).into());
7477
}
7578
}
7679
}
@@ -85,7 +88,7 @@ impl KeepAwake {
8588
&mut self.sleep_assertion,
8689
);
8790
if result != kIOReturnSuccess as i32 {
88-
return Err(anyhow!("IO error: {:#x}", result));
91+
return Err(format!("IO error: {:#x}", result).into());
8992
}
9093
}
9194
}

src/sys/windows.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//! [`SetThreadExecutionState`]: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
88
//! [`PowerSetRequest`]: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-powersetrequest
99
10-
use anyhow::Result;
1110
use windows::core::Error as WindowsError;
1211
use windows::Win32::System::Power::{
1312
SetThreadExecutionState, ES_AWAYMODE_REQUIRED, ES_CONTINUOUS, ES_DISPLAY_REQUIRED,
@@ -16,13 +15,15 @@ use windows::Win32::System::Power::{
1615

1716
use crate::Options;
1817

18+
pub type Error = WindowsError;
19+
1920
pub struct KeepAwake {
2021
options: Options,
2122
previous: EXECUTION_STATE,
2223
}
2324

2425
impl KeepAwake {
25-
pub fn new(options: Options) -> Result<Self> {
26+
pub fn new(options: Options) -> Result<Self, Error> {
2627
let mut awake = KeepAwake {
2728
options,
2829
previous: Default::default(),
@@ -31,7 +32,7 @@ impl KeepAwake {
3132
Ok(awake)
3233
}
3334

34-
fn set(&mut self) -> Result<(), WindowsError> {
35+
fn set(&mut self) -> Result<(), Error> {
3536
let mut esflags = ES_CONTINUOUS;
3637

3738
if self.options.display {

0 commit comments

Comments
 (0)