Skip to content

Commit

Permalink
Try switching to thiserror #2
Browse files Browse the repository at this point in the history
  • Loading branch information
segevfiner committed Jan 19, 2024
1 parent 4aabf58 commit 3b23375
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
12 changes: 10 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"
]

[profile.release]
strip = true
Expand All @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
//! # try_main();
//! ```
use std::error;

use derive_builder::Builder;
use thiserror::Error;

Expand All @@ -38,7 +40,7 @@ pub enum Error {
Builder(#[from] BuilderError),

#[error("system: {0}")]
System(#[from] anyhow::Error),
System(#[from] Box<dyn error::Error + Send + Sync>),
}

#[derive(Builder, Debug)]
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
9 changes: 4 additions & 5 deletions src/sys/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -42,7 +41,7 @@ impl KeepAwake {
Ok(awake)
}

fn set(&mut self) -> Result<()> {
fn set(&mut self) -> Result<(), Box<dyn error::Error + Send + Sync>> {

Check failure on line 44 in src/sys/macos.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

failed to resolve: use of undeclared crate or module `error`
if self.options.display {
unsafe {
let result = IOPMAssertionCreateWithName(
Expand All @@ -56,7 +55,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 @@ -72,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 @@ -88,7 +87,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

0 comments on commit 3b23375

Please sign in to comment.