@@ -10,7 +10,7 @@ use bytemuck::{Pod, Zeroable, __core::time::Duration};
10
10
use esp_idf_part:: PartitionTable ;
11
11
use log:: { debug, info, warn} ;
12
12
use serialport:: UsbPortInfo ;
13
- use strum:: { Display , EnumVariantNames } ;
13
+ use strum:: { Display , EnumIter , EnumVariantNames } ;
14
14
15
15
use self :: stubs:: FlashStub ;
16
16
use crate :: {
@@ -134,7 +134,7 @@ impl FromStr for FlashMode {
134
134
/// Supported flash sizes
135
135
///
136
136
/// Note that not all sizes are supported by each target device.
137
- #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Display , EnumVariantNames ) ]
137
+ #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Display , EnumVariantNames , EnumIter ) ]
138
138
#[ repr( u8 ) ]
139
139
pub enum FlashSize {
140
140
/// 256 KB
@@ -208,21 +208,15 @@ impl FromStr for FlashSize {
208
208
type Err = Error ;
209
209
210
210
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
211
- use FlashSize :: * ;
212
-
213
- match s. to_uppercase ( ) . as_str ( ) {
214
- "256KB" => Ok ( Flash256Kb ) ,
215
- "512KB" => Ok ( Flash512Kb ) ,
216
- "1MB" => Ok ( Flash1Mb ) ,
217
- "2MB" => Ok ( Flash2Mb ) ,
218
- "4MB" => Ok ( Flash4Mb ) ,
219
- "8MB" => Ok ( Flash8Mb ) ,
220
- "16MB" => Ok ( Flash16Mb ) ,
221
- "32MB" => Ok ( Flash32Mb ) ,
222
- "64MB" => Ok ( Flash64Mb ) ,
223
- "128MB" => Ok ( Flash128Mb ) ,
224
- _ => Err ( Error :: InvalidFlashSize ( s. to_string ( ) ) ) ,
225
- }
211
+ use strum:: { IntoEnumIterator , VariantNames } ;
212
+ let upper = s. to_uppercase ( ) ;
213
+ FlashSize :: VARIANTS
214
+ . iter ( )
215
+ . copied ( )
216
+ . zip ( FlashSize :: iter ( ) )
217
+ . find ( |( name, _) | * name == upper)
218
+ . map ( |( _, variant) | variant)
219
+ . ok_or_else ( || Error :: InvalidFlashSize ( s. to_string ( ) ) )
226
220
}
227
221
}
228
222
0 commit comments