@@ -22,7 +22,6 @@ use indicatif::{style::ProgressStyle, HumanCount, ProgressBar, ProgressDrawTarge
22
22
use log:: { debug, info} ;
23
23
use miette:: { IntoDiagnostic , Result , WrapErr } ;
24
24
use serialport:: { SerialPortType , UsbPortInfo } ;
25
- use strum:: VariantNames ;
26
25
27
26
use self :: { config:: Config , monitor:: monitor, serial:: get_serial_port_info} ;
28
27
use crate :: {
@@ -39,26 +38,6 @@ pub mod monitor;
39
38
40
39
mod serial;
41
40
42
- // Since as of `[email protected] ` the `possible_values` attribute is no longer
43
- // present, we must use the more convoluted `value_parser` attribute instead.
44
- // Since this is a bit tedious, we'll use a helper macro to abstract away all
45
- // the cruft. It's important to note that this macro assumes the
46
- // `strum::EnumVariantNames` trait has been implemented for the provided type,
47
- // and that the provided type is in scope when calling this macro.
48
- //
49
- // See this comment for details:
50
- // https://github.com/clap-rs/clap/discussions/4264#discussioncomment-3737696
51
- #[ doc( hidden) ]
52
- #[ macro_export]
53
- macro_rules! clap_enum_variants {
54
- ( $e: ty) => { {
55
- use clap:: builder:: TypedValueParser ;
56
- clap:: builder:: PossibleValuesParser :: new( <$e>:: VARIANTS ) . map( |s| s. parse:: <$e>( ) . unwrap( ) )
57
- } } ;
58
- }
59
-
60
- pub use clap_enum_variants;
61
-
62
41
/// Establish a connection with a target device
63
42
#[ derive( Debug , Args ) ]
64
43
pub struct ConnectArgs {
@@ -85,13 +64,13 @@ pub struct ConnectArgs {
85
64
#[ derive( Debug , Args ) ]
86
65
pub struct FlashConfigArgs {
87
66
/// Flash frequency
88
- #[ arg( short = 'f' , long, value_name = "FREQ" , value_parser = clap_enum_variants! ( FlashFrequency ) ) ]
67
+ #[ arg( short = 'f' , long, value_name = "FREQ" , value_enum ) ]
89
68
pub flash_freq : Option < FlashFrequency > ,
90
69
/// Flash mode to use
91
- #[ arg( short = 'm' , long, value_name = "MODE" , value_parser = clap_enum_variants! ( FlashMode ) ) ]
70
+ #[ arg( short = 'm' , long, value_name = "MODE" , value_enum ) ]
92
71
pub flash_mode : Option < FlashMode > ,
93
72
/// Flash size of the target
94
- #[ arg( short = 's' , long, value_name = "SIZE" , value_parser = clap_enum_variants! ( FlashSize ) ) ]
73
+ #[ arg( short = 's' , long, value_name = "SIZE" , value_enum ) ]
95
74
pub flash_size : Option < FlashSize > ,
96
75
}
97
76
@@ -111,10 +90,16 @@ pub struct FlashArgs {
111
90
) ]
112
91
pub erase_parts : Option < Vec < String > > ,
113
92
/// Erase specified data partitions
114
- #[ arg( long, requires = "partition_table" , value_name = "PARTS" , value_parser = clap_enum_variants!( DataType ) , value_delimiter = ',' ) ]
93
+ #[ arg(
94
+ long,
95
+ requires = "partition_table" ,
96
+ value_name = "PARTS" ,
97
+ value_enum,
98
+ value_delimiter = ','
99
+ ) ]
115
100
pub erase_data_parts : Option < Vec < DataType > > ,
116
101
/// Image format to flash
117
- #[ arg( long, value_parser = clap_enum_variants! ( ImageFormatKind ) ) ]
102
+ #[ arg( long, value_enum ) ]
118
103
pub format : Option < ImageFormatKind > ,
119
104
/// Open a serial monitor after flashing
120
105
#[ arg( short = 'M' , long) ]
@@ -155,7 +140,7 @@ pub struct SaveImageArgs {
155
140
#[ arg( long, value_name = "FILE" ) ]
156
141
pub bootloader : Option < PathBuf > ,
157
142
/// Chip to create an image for
158
- #[ arg( long, value_parser = clap_enum_variants! ( Chip ) ) ]
143
+ #[ arg( long, value_enum ) ]
159
144
pub chip : Chip ,
160
145
/// File name to save the generated image to
161
146
pub file : PathBuf ,
@@ -193,16 +178,14 @@ where
193
178
_ => ProgressDrawTarget :: hidden ( ) ,
194
179
} ;
195
180
196
- let progress = ProgressBar :: with_draw_target ( len, draw_target)
181
+ ProgressBar :: with_draw_target ( len, draw_target)
197
182
. with_message ( msg)
198
183
. with_style (
199
184
ProgressStyle :: default_bar ( )
200
185
. template ( "[{elapsed_precise}] [{bar:40}] {pos:>7}/{len:7} {msg}" )
201
186
. unwrap ( )
202
187
. progress_chars ( "=> " ) ,
203
- ) ;
204
-
205
- progress
188
+ )
206
189
}
207
190
208
191
/// Create a callback function for the provided [ProgressBar]
@@ -277,7 +260,7 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
277
260
278
261
/// Connect to a target device and print information about its chip
279
262
pub fn board_info ( args : & ConnectArgs , config : & Config ) -> Result < ( ) > {
280
- let mut flasher = connect ( & args, config) ?;
263
+ let mut flasher = connect ( args, config) ?;
281
264
print_board_info ( & mut flasher) ?;
282
265
283
266
Ok ( ( ) )
@@ -291,7 +274,7 @@ pub fn print_board_info(flasher: &mut Flasher) -> Result<()> {
291
274
if let Some ( ( major, minor) ) = info. revision {
292
275
println ! ( " (revision v{major}.{minor})" ) ;
293
276
} else {
294
- println ! ( "" ) ;
277
+ println ! ( ) ;
295
278
}
296
279
println ! ( "Crystal frequency: {}MHz" , info. crystal_frequency) ;
297
280
println ! ( "Flash size: {}" , info. flash_size) ;
@@ -321,7 +304,7 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
321
304
// The 26MHz ESP32-C2's need to be treated as a special case.
322
305
let default_baud = if chip == Chip :: Esp32c2
323
306
&& !args. connect_args . use_stub
324
- && target. crystal_freq ( & mut flasher. connection ( ) ) ? == 26
307
+ && target. crystal_freq ( flasher. connection ( ) ) ? == 26
325
308
{
326
309
74_880
327
310
} else {
@@ -537,7 +520,7 @@ pub fn erase_partitions(
537
520
for label in part_labels {
538
521
let part = partition_table
539
522
. find ( label. as_str ( ) )
540
- . ok_or ( MissingPartition :: from ( label) ) ?;
523
+ . ok_or_else ( || MissingPartition :: from ( label) ) ?;
541
524
542
525
parts_to_erase
543
526
. get_or_insert ( HashMap :: new ( ) )
@@ -648,12 +631,12 @@ fn pretty_print(table: PartitionTable) {
648
631
649
632
for p in table. partitions ( ) {
650
633
pretty. add_row ( vec ! [
651
- Cell :: new( & p. name( ) ) . fg( Color :: Green ) ,
652
- Cell :: new( & p. ty( ) . to_string( ) ) . fg( Color :: Cyan ) ,
653
- Cell :: new( & p. subtype( ) . to_string( ) ) . fg( Color :: Magenta ) ,
654
- Cell :: new( & format!( "{:#x}" , p. offset( ) ) ) . fg( Color :: Red ) ,
655
- Cell :: new( & format!( "{:#x} ({}KiB)" , p. size( ) , p. size( ) / 1024 ) ) . fg( Color :: Yellow ) ,
656
- Cell :: new( & p. encrypted( ) ) . fg( Color :: DarkCyan ) ,
634
+ Cell :: new( p. name( ) ) . fg( Color :: Green ) ,
635
+ Cell :: new( p. ty( ) . to_string( ) ) . fg( Color :: Cyan ) ,
636
+ Cell :: new( p. subtype( ) . to_string( ) ) . fg( Color :: Magenta ) ,
637
+ Cell :: new( format!( "{:#x}" , p. offset( ) ) ) . fg( Color :: Red ) ,
638
+ Cell :: new( format!( "{:#x} ({}KiB)" , p. size( ) , p. size( ) / 1024 ) ) . fg( Color :: Yellow ) ,
639
+ Cell :: new( p. encrypted( ) ) . fg( Color :: DarkCyan ) ,
657
640
] ) ;
658
641
}
659
642
0 commit comments