Skip to content

Commit

Permalink
Better macOS system error type
Browse files Browse the repository at this point in the history
  • Loading branch information
segevfiner committed Feb 11, 2024
1 parent ba244dc commit a4ae096
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/sys/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
//!
//! [`IOPMAssertionCreateWithName`]: https://developer.apple.com/documentation/iokit/1557134-iopmassertioncreatewithname

use std::error;

use apple_sys::IOKit::{
kIOPMAssertionLevelOn, kIOReturnSuccess, CFStringRef, IOPMAssertionCreateWithName,

Check warning on line 8 in src/sys/macos.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Diff in /home/runner/work/keepawake-rs/keepawake-rs/src/sys/macos.rs

Check warning on line 8 in src/sys/macos.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Diff in \\?\D:\a\keepawake-rs\keepawake-rs\src\sys\macos.rs

Check warning on line 8 in src/sys/macos.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Diff in /Users/runner/work/keepawake-rs/keepawake-rs/src/sys/macos.rs
IOPMAssertionRelease,
};
use thiserror::Error;
use core_foundation::{base::TCFType, string::CFString};

use crate::Options;

pub type Error = Box<dyn error::Error + Send + Sync>;
#[derive(Error, Debug)]
#[error("IO error: {code:#010x}")]
pub struct IOError {
code: i32,
}

pub type Error = IOError;

#[allow(non_upper_case_globals)]
const kIOPMAssertionTypePreventUserIdleSystemSleep: &str = "PreventUserIdleSystemSleep";
Expand Down Expand Up @@ -57,8 +62,7 @@ impl KeepAwake {
&mut self.display_assertion,
);
if result != kIOReturnSuccess as i32 {
// TODO Better error?
return Err(format!("IO error: {:#x}", result).into());
return Err(Error { code: result });
}
}
}
Expand All @@ -73,7 +77,7 @@ impl KeepAwake {
&mut self.idle_assertion,
);
if result != kIOReturnSuccess as i32 {
return Err(format!("IO error: {:#x}", result).into());
return Err(Error { code: result });
}
}
}
Expand All @@ -88,7 +92,7 @@ impl KeepAwake {
&mut self.sleep_assertion,
);
if result != kIOReturnSuccess as i32 {
return Err(format!("IO error: {:#x}", result).into());
return Err(Error { code: result });
}
}
}
Expand Down

0 comments on commit a4ae096

Please sign in to comment.