Skip to content

Commit a4ae096

Browse files
committed
Better macOS system error type
1 parent ba244dc commit a4ae096

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/sys/macos.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
//!
55
//! [`IOPMAssertionCreateWithName`]: https://developer.apple.com/documentation/iokit/1557134-iopmassertioncreatewithname
66
7-
use std::error;
8-
97
use apple_sys::IOKit::{
108
kIOPMAssertionLevelOn, kIOReturnSuccess, CFStringRef, IOPMAssertionCreateWithName,
119
IOPMAssertionRelease,
1210
};
11+
use thiserror::Error;
1312
use core_foundation::{base::TCFType, string::CFString};
1413

1514
use crate::Options;
1615

17-
pub type Error = Box<dyn error::Error + Send + Sync>;
16+
#[derive(Error, Debug)]
17+
#[error("IO error: {code:#010x}")]
18+
pub struct IOError {
19+
code: i32,
20+
}
21+
22+
pub type Error = IOError;
1823

1924
#[allow(non_upper_case_globals)]
2025
const kIOPMAssertionTypePreventUserIdleSystemSleep: &str = "PreventUserIdleSystemSleep";
@@ -57,8 +62,7 @@ impl KeepAwake {
5762
&mut self.display_assertion,
5863
);
5964
if result != kIOReturnSuccess as i32 {
60-
// TODO Better error?
61-
return Err(format!("IO error: {:#x}", result).into());
65+
return Err(Error { code: result });
6266
}
6367
}
6468
}
@@ -73,7 +77,7 @@ impl KeepAwake {
7377
&mut self.idle_assertion,
7478
);
7579
if result != kIOReturnSuccess as i32 {
76-
return Err(format!("IO error: {:#x}", result).into());
80+
return Err(Error { code: result });
7781
}
7882
}
7983
}
@@ -88,7 +92,7 @@ impl KeepAwake {
8892
&mut self.sleep_assertion,
8993
);
9094
if result != kIOReturnSuccess as i32 {
91-
return Err(format!("IO error: {:#x}", result).into());
95+
return Err(Error { code: result });
9296
}
9397
}
9498
}

0 commit comments

Comments
 (0)