4
4
//!
5
5
//! [`IOPMAssertionCreateWithName`]: https://developer.apple.com/documentation/iokit/1557134-iopmassertioncreatewithname
6
6
7
- use std:: error;
8
-
9
7
use apple_sys:: IOKit :: {
10
8
kIOPMAssertionLevelOn, kIOReturnSuccess, CFStringRef , IOPMAssertionCreateWithName ,
11
9
IOPMAssertionRelease ,
12
10
} ;
11
+ use thiserror:: Error ;
13
12
use core_foundation:: { base:: TCFType , string:: CFString } ;
14
13
15
14
use crate :: Options ;
16
15
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 ;
18
23
19
24
#[ allow( non_upper_case_globals) ]
20
25
const kIOPMAssertionTypePreventUserIdleSystemSleep: & str = "PreventUserIdleSystemSleep" ;
@@ -57,8 +62,7 @@ impl KeepAwake {
57
62
& mut self . display_assertion ,
58
63
) ;
59
64
if result != kIOReturnSuccess as i32 {
60
- // TODO Better error?
61
- return Err ( format ! ( "IO error: {:#x}" , result) . into ( ) ) ;
65
+ return Err ( Error { code : result } ) ;
62
66
}
63
67
}
64
68
}
@@ -73,7 +77,7 @@ impl KeepAwake {
73
77
& mut self . idle_assertion ,
74
78
) ;
75
79
if result != kIOReturnSuccess as i32 {
76
- return Err ( format ! ( "IO error: {:#x}" , result) . into ( ) ) ;
80
+ return Err ( Error { code : result } ) ;
77
81
}
78
82
}
79
83
}
@@ -88,7 +92,7 @@ impl KeepAwake {
88
92
& mut self . sleep_assertion ,
89
93
) ;
90
94
if result != kIOReturnSuccess as i32 {
91
- return Err ( format ! ( "IO error: {:#x}" , result) . into ( ) ) ;
95
+ return Err ( Error { code : result } ) ;
92
96
}
93
97
}
94
98
}
0 commit comments