11//! Module for the [`BootPolicy`] helper type.
22
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 } ;
254
265/// The UEFI boot policy is a property that influences the behaviour of
276/// various UEFI functions that load files (typically UEFI images).
287///
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`] .
3110#[ derive( Copy , Clone , Debug , Default , Eq , Ord , PartialEq , PartialOrd ) ]
3211pub enum BootPolicy {
3312 /// Indicates that the request originates from the boot manager, and that
@@ -36,50 +15,34 @@ pub enum BootPolicy {
3615 ///
3716 /// Boot selection refers to what a user has chosen in the (GUI) boot menu.
3817 ///
39- /// This corresponds to the `TRUE` value in the UEFI spec .
18+ /// This corresponds to the underlying [`Boolean`] being `true` .
4019 BootSelection ,
4120 /// The provided `file_path` must match an exact file to be loaded.
4221 ///
43- /// This corresponds to the `FALSE` value in the UEFI spec .
22+ /// This corresponds to the underlying [`Boolean`] being `false` .
4423 #[ default]
4524 ExactMatch ,
4625}
4726
48- impl From < BootPolicy > for bool {
27+ impl From < BootPolicy > for Boolean {
4928 fn from ( value : BootPolicy ) -> Self {
5029 match value {
51- BootPolicy :: BootSelection => true ,
52- BootPolicy :: ExactMatch => false ,
30+ BootPolicy :: BootSelection => true . into ( ) ,
31+ BootPolicy :: ExactMatch => false . into ( ) ,
5332 }
5433 }
5534}
5635
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 {
6042 true => Self :: BootSelection ,
6143 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)
8346 }
8447}
8548
@@ -89,20 +52,15 @@ mod tests {
8952
9053 #[ test]
9154 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 ) ) ;
10355 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
10662 ) ;
63+ assert_eq ! ( Boolean :: from( BootPolicy :: BootSelection ) , Boolean :: TRUE ) ;
64+ assert_eq ! ( Boolean :: from( BootPolicy :: ExactMatch ) , Boolean :: FALSE ) ;
10765 }
10866}
0 commit comments