Skip to content

Commit 20c250d

Browse files
zRedShiftjessebraham
authored andcommitted
fix FlashSize discrepancy between clap, strum and FromStr
1 parent fb0877e commit 20c250d

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

espflash/src/flasher/mod.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bytemuck::{Pod, Zeroable, __core::time::Duration};
1010
use esp_idf_part::PartitionTable;
1111
use log::{debug, info, warn};
1212
use serialport::UsbPortInfo;
13-
use strum::{Display, EnumVariantNames};
13+
use strum::{Display, EnumIter, EnumVariantNames};
1414

1515
use self::stubs::FlashStub;
1616
use crate::{
@@ -134,7 +134,7 @@ impl FromStr for FlashMode {
134134
/// Supported flash sizes
135135
///
136136
/// 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)]
138138
#[repr(u8)]
139139
pub enum FlashSize {
140140
/// 256 KB
@@ -208,21 +208,15 @@ impl FromStr for FlashSize {
208208
type Err = Error;
209209

210210
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()))
226220
}
227221
}
228222

0 commit comments

Comments
 (0)