3
3
use anyhow:: anyhow;
4
4
use lpc55:: bootloader:: Bootloader ;
5
5
6
- use core:: fmt;
7
6
use crate :: { Card , Result , Uuid } ;
7
+ use core:: fmt;
8
8
9
9
// #[derive(Debug, Eq, PartialEq)]
10
10
pub enum Device {
11
11
Bootloader ( Bootloader ) ,
12
12
Card ( Card ) ,
13
13
}
14
14
15
-
16
15
impl fmt:: Display for Device {
17
16
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
18
17
match self {
19
- Device :: Bootloader ( bootloader) =>
20
- f. write_fmt ( format_args ! ( "Bootloader UUID: {}" , Uuid :: from( bootloader. uuid) . hex( ) ) ) ,
18
+ Device :: Bootloader ( bootloader) => f. write_fmt ( format_args ! (
19
+ "Bootloader UUID: {}" ,
20
+ Uuid :: from( bootloader. uuid) . hex( )
21
+ ) ) ,
21
22
Device :: Card ( card) => card. fmt ( f) ,
22
23
}
23
24
}
@@ -26,18 +27,21 @@ impl fmt::Display for Device {
26
27
impl Device {
27
28
pub fn list ( ) -> Vec < Self > {
28
29
let bootloaders = Bootloader :: list ( ) . into_iter ( ) . map ( Device :: from) ;
29
- let cards = Card :: list ( crate :: smartcard:: Filter :: SoloCards ) . into_iter ( ) . map ( Device :: from) ;
30
+ let cards = Card :: list ( crate :: smartcard:: Filter :: SoloCards )
31
+ . into_iter ( )
32
+ . map ( Device :: from) ;
30
33
31
- let devices = bootloaders. chain ( cards) . collect ( ) ;
32
- devices
34
+ bootloaders. chain ( cards) . collect ( )
33
35
}
34
36
35
37
/// If this is a Solo device, this will successfully report the UUID.
36
38
/// Not guaranteed to work with other devices.
37
39
pub fn uuid ( & self ) -> Result < Uuid > {
38
40
match self {
39
41
Device :: Bootloader ( bootloader) => Ok ( bootloader. uuid . into ( ) ) ,
40
- Device :: Card ( card) => card. uuid . ok_or ( anyhow ! ( "Device does not have a UUID" ) ) ,
42
+ Device :: Card ( card) => card
43
+ . uuid
44
+ . ok_or_else ( || anyhow ! ( "Device does not have a UUID" ) ) ,
41
45
}
42
46
}
43
47
@@ -79,7 +83,10 @@ pub fn find_bootloader(uuid: Option<Uuid>) -> Result<Bootloader> {
79
83
return Ok ( bootloader) ;
80
84
}
81
85
}
82
- return Err ( anyhow ! ( "Could not find any Solo 2 device with uuid {}." , uuid. hex( ) ) ) ;
86
+ return Err ( anyhow ! (
87
+ "Could not find any Solo 2 device with uuid {}." ,
88
+ uuid. hex( )
89
+ ) ) ;
83
90
} else {
84
91
let mut devices: Vec < Device > = Default :: default ( ) ;
85
92
for bootloader in bootloaders {
@@ -97,35 +104,36 @@ pub fn prompt_user_to_select_device(mut devices: Vec<Device>) -> Result<Device>
97
104
return Err ( anyhow ! ( "No Solo 2 devices connected" ) ) ;
98
105
}
99
106
100
- let items: Vec < String > = devices. iter ( ) . map ( |device| {
101
- match device {
102
- Device :: Bootloader ( bootloader) => {
103
- format ! (
104
- "Bootloader UUID: {}" ,
105
- hex:: encode( bootloader. uuid. to_be_bytes( ) )
106
- )
107
-
108
- } ,
109
- Device :: Card ( card) => {
110
- if let Some ( uuid) = card. uuid {
111
- // format!("\"{}\" UUID: {}", card.reader_name, hex::encode(uuid.to_be_bytes()))
112
- format ! ( "Solo 2 {}" , uuid. hex( ) )
113
- } else {
114
- format ! ( " \" {}\" " , card. reader_name)
107
+ let items: Vec < String > = devices
108
+ . iter ( )
109
+ . map ( |device| {
110
+ match device {
111
+ Device :: Bootloader ( bootloader) => {
112
+ format ! (
113
+ "Bootloader UUID: {}" ,
114
+ hex:: encode( bootloader. uuid. to_be_bytes( ) )
115
+ )
116
+ }
117
+ Device :: Card ( card) => {
118
+ if let Some ( uuid) = card. uuid {
119
+ // format!("\"{}\" UUID: {}", card.reader_name, hex::encode(uuid.to_be_bytes()))
120
+ format ! ( "Solo 2 {}" , uuid. hex( ) )
121
+ } else {
122
+ format ! ( " \" {}\" " , card. reader_name)
123
+ }
115
124
}
116
125
}
117
- }
118
- } ) . collect ( ) ;
126
+ } )
127
+ . collect ( ) ;
119
128
120
- use dialoguer:: { Select , theme } ;
129
+ use dialoguer:: { theme , Select } ;
121
130
// let selection = Select::with_theme(&theme::SimpleTheme)
122
131
let selection = Select :: with_theme ( & theme:: ColorfulTheme :: default ( ) )
123
132
. with_prompt ( "Multiple Solo 2 devices connected, select one or hit Escape key" )
124
133
. items ( & items)
125
134
. default ( 0 )
126
135
. interact_opt ( ) ?
127
- . ok_or ( anyhow ! ( "No device selected" ) ) ?;
136
+ . ok_or_else ( || anyhow ! ( "No device selected" ) ) ?;
128
137
129
138
Ok ( devices. remove ( selection) )
130
-
131
139
}
0 commit comments