1
1
//! Module for the [`BootPolicy`] helper type.
2
2
3
- use core:: fmt:: { Display , Formatter } ;
4
-
5
- /// Errors that can happen when working with [`BootPolicy`].
6
- #[ derive( Debug , Copy , Clone , PartialOrd , PartialEq , Eq , Ord ) ]
7
- pub enum BootPolicyError {
8
- /// Only `0` and `1` are valid integers, all other values are undefined.
9
- InvalidInteger ( u8 ) ,
10
- }
11
-
12
- impl Display for BootPolicyError {
13
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
14
- let s = match self {
15
- Self :: InvalidInteger ( _) => {
16
- "Only `0` and `1` are valid integers, all other values are undefined."
17
- }
18
- } ;
19
- f. write_str ( s)
20
- }
21
- }
22
-
23
- #[ cfg( feature = "unstable" ) ]
24
- impl core:: error:: Error for BootPolicyError { }
3
+ use uefi_raw:: { Boolean , InvalidBooleanError } ;
25
4
26
5
/// The UEFI boot policy is a property that influences the behaviour of
27
6
/// various UEFI functions that load files (typically UEFI images).
28
7
///
29
- /// This type is not ABI compatible. On the ABI level, this is an UEFI
30
- /// boolean .
8
+ /// This type is not ABI compatible. On the ABI level, this corresponds to
9
+ /// a [`Boolean`] .
31
10
#[ derive( Copy , Clone , Debug , Default , Eq , Ord , PartialEq , PartialOrd ) ]
32
11
pub enum BootPolicy {
33
12
/// Indicates that the request originates from the boot manager, and that
@@ -36,50 +15,34 @@ pub enum BootPolicy {
36
15
///
37
16
/// Boot selection refers to what a user has chosen in the (GUI) boot menu.
38
17
///
39
- /// This corresponds to the `TRUE` value in the UEFI spec .
18
+ /// This corresponds to the underlying [`Boolean`] being `true` .
40
19
BootSelection ,
41
20
/// The provided `file_path` must match an exact file to be loaded.
42
21
///
43
- /// This corresponds to the `FALSE` value in the UEFI spec .
22
+ /// This corresponds to the underlying [`Boolean`] being `false` .
44
23
#[ default]
45
24
ExactMatch ,
46
25
}
47
26
48
- impl From < BootPolicy > for bool {
27
+ impl From < BootPolicy > for Boolean {
49
28
fn from ( value : BootPolicy ) -> Self {
50
29
match value {
51
- BootPolicy :: BootSelection => true ,
52
- BootPolicy :: ExactMatch => false ,
30
+ BootPolicy :: BootSelection => true . into ( ) ,
31
+ BootPolicy :: ExactMatch => false . into ( ) ,
53
32
}
54
33
}
55
34
}
56
35
57
- impl From < bool > for BootPolicy {
58
- fn from ( value : bool ) -> Self {
59
- match value {
36
+ impl TryFrom < Boolean > for BootPolicy {
37
+ type Error = InvalidBooleanError ;
38
+
39
+ fn try_from ( value : Boolean ) -> Result < Self , Self :: Error > {
40
+ let boolean: bool = value. try_into ( ) ?;
41
+ let policy = match boolean {
60
42
true => Self :: BootSelection ,
61
43
false => Self :: ExactMatch ,
62
- }
63
- }
64
- }
65
-
66
- impl From < BootPolicy > for u8 {
67
- fn from ( value : BootPolicy ) -> Self {
68
- match value {
69
- BootPolicy :: BootSelection => 1 ,
70
- BootPolicy :: ExactMatch => 0 ,
71
- }
72
- }
73
- }
74
-
75
- impl TryFrom < u8 > for BootPolicy {
76
- type Error = BootPolicyError ;
77
- fn try_from ( value : u8 ) -> Result < Self , Self :: Error > {
78
- match value {
79
- 0 => Ok ( Self :: ExactMatch ) ,
80
- 1 => Ok ( Self :: BootSelection ) ,
81
- err => Err ( Self :: Error :: InvalidInteger ( err) ) ,
82
- }
44
+ } ;
45
+ Ok ( policy)
83
46
}
84
47
}
85
48
@@ -89,20 +52,15 @@ mod tests {
89
52
90
53
#[ test]
91
54
fn boot_policy ( ) {
92
- assert_eq ! ( bool :: from( BootPolicy :: ExactMatch ) , false ) ;
93
- assert_eq ! ( bool :: from( BootPolicy :: BootSelection ) , true ) ;
94
-
95
- assert_eq ! ( BootPolicy :: from( false ) , BootPolicy :: ExactMatch ) ;
96
- assert_eq ! ( BootPolicy :: from( true ) , BootPolicy :: BootSelection ) ;
97
-
98
- assert_eq ! ( u8 :: from( BootPolicy :: ExactMatch ) , 0 ) ;
99
- assert_eq ! ( u8 :: from( BootPolicy :: BootSelection ) , 1 ) ;
100
-
101
- assert_eq ! ( BootPolicy :: try_from( 0 ) , Ok ( BootPolicy :: ExactMatch ) ) ;
102
- assert_eq ! ( BootPolicy :: try_from( 1 ) , Ok ( BootPolicy :: BootSelection ) ) ;
103
55
assert_eq ! (
104
- BootPolicy :: try_from( 2 ) ,
105
- Err ( BootPolicyError :: InvalidInteger ( 2 ) )
56
+ BootPolicy :: try_from( Boolean :: TRUE ) . unwrap( ) ,
57
+ BootPolicy :: BootSelection
58
+ ) ;
59
+ assert_eq ! (
60
+ BootPolicy :: try_from( Boolean :: FALSE ) . unwrap( ) ,
61
+ BootPolicy :: ExactMatch
106
62
) ;
63
+ assert_eq ! ( Boolean :: from( BootPolicy :: BootSelection ) , Boolean :: TRUE ) ;
64
+ assert_eq ! ( Boolean :: from( BootPolicy :: ExactMatch ) , Boolean :: FALSE ) ;
107
65
}
108
66
}
0 commit comments