Skip to content

Commit c35fad9

Browse files
committed
Add ImageFormatType enum, normalize other enums and their trait impls
1 parent fa4f269 commit c35fad9

File tree

4 files changed

+64
-39
lines changed

4 files changed

+64
-39
lines changed

espflash/src/chip/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl SpiRegisters {
151151
}
152152
}
153153

154-
#[derive(Debug, Copy, Clone, Eq, PartialEq, Display, EnumVariantNames, EnumIter)]
154+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumIter, EnumVariantNames)]
155155
pub enum Chip {
156156
#[strum(serialize = "ESP32")]
157157
Esp32,
@@ -171,13 +171,15 @@ impl FromStr for Chip {
171171
type Err = Error;
172172

173173
fn from_str(s: &str) -> Result<Self, Self::Err> {
174+
use Chip::*;
175+
174176
match s.to_lowercase().replace('-', "").as_str() {
175-
"esp32" => Ok(Chip::Esp32),
176-
"esp32c2" => Ok(Chip::Esp32c2),
177-
"esp32c3" => Ok(Chip::Esp32c3),
178-
"esp32s2" => Ok(Chip::Esp32s2),
179-
"esp32s3" => Ok(Chip::Esp32s3),
180-
"esp8266" => Ok(Chip::Esp8266),
177+
"esp32" => Ok(Esp32),
178+
"esp32c2" => Ok(Esp32c2),
179+
"esp32c3" => Ok(Esp32c3),
180+
"esp32s2" => Ok(Esp32s2),
181+
"esp32s3" => Ok(Esp32s3),
182+
"esp8266" => Ok(Esp8266),
181183
_ => Err(Error::UnrecognizedChipName),
182184
}
183185
}

espflash/src/elf.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl FromStr for FlashMode {
4646
}
4747
}
4848

49-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display, EnumVariantNames)]
49+
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Display, EnumVariantNames)]
5050
#[repr(u8)]
5151
pub enum FlashFrequency {
5252
#[strum(serialize = "12M")]
@@ -79,22 +79,20 @@ impl FromStr for FlashFrequency {
7979
fn from_str(s: &str) -> Result<Self, Self::Err> {
8080
use FlashFrequency::*;
8181

82-
let freq = match s.to_uppercase().as_str() {
83-
"12M" => Flash12M,
84-
"15M" => Flash15M,
85-
"16M" => Flash16M,
86-
"20M" => Flash20M,
87-
"24M" => Flash24M,
88-
"26M" => Flash26M,
89-
"30M" => Flash30M,
90-
"40M" => Flash40M,
91-
"48M" => Flash48M,
92-
"60M" => Flash60M,
93-
"80M" => Flash80M,
94-
_ => return Err(Error::InvalidFlashFrequency(s.to_string())),
95-
};
96-
97-
Ok(freq)
82+
match s.to_uppercase().as_str() {
83+
"12M" => Ok(Flash12M),
84+
"15M" => Ok(Flash15M),
85+
"16M" => Ok(Flash16M),
86+
"20M" => Ok(Flash20M),
87+
"24M" => Ok(Flash24M),
88+
"26M" => Ok(Flash26M),
89+
"30M" => Ok(Flash30M),
90+
"40M" => Ok(Flash40M),
91+
"48M" => Ok(Flash48M),
92+
"60M" => Ok(Flash60M),
93+
"80M" => Ok(Flash80M),
94+
_ => Err(Error::InvalidFlashFrequency(s.to_string())),
95+
}
9896
}
9997
}
10098

espflash/src/flasher.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ impl FromStr for FlashSize {
9191
type Err = Error;
9292

9393
fn from_str(s: &str) -> Result<Self, Self::Err> {
94-
let size = match s.to_uppercase().as_str() {
95-
"256KB" => FlashSize::Flash256Kb,
96-
"512KB" => FlashSize::Flash512Kb,
97-
"1MB" => FlashSize::Flash1Mb,
98-
"2MB" => FlashSize::Flash2Mb,
99-
"4MB" => FlashSize::Flash4Mb,
100-
"8MB" => FlashSize::Flash8Mb,
101-
"16MB" => FlashSize::Flash16Mb,
102-
"32MB" => FlashSize::Flash32Mb,
103-
"64MB" => FlashSize::Flash64Mb,
104-
"128MB" => FlashSize::Flash128Mb,
105-
_ => return Err(Error::InvalidFlashSize(s.to_string())),
106-
};
107-
108-
Ok(size)
94+
use FlashSize::*;
95+
96+
match s.to_uppercase().as_str() {
97+
"256KB" => Ok(Flash256Kb),
98+
"512KB" => Ok(Flash512Kb),
99+
"1MB" => Ok(Flash1Mb),
100+
"2MB" => Ok(Flash2Mb),
101+
"4MB" => Ok(Flash4Mb),
102+
"8MB" => Ok(Flash8Mb),
103+
"16MB" => Ok(Flash16Mb),
104+
"32MB" => Ok(Flash32Mb),
105+
"64MB" => Ok(Flash64Mb),
106+
"128MB" => Ok(Flash128Mb),
107+
_ => Err(Error::InvalidFlashSize(s.to_string())),
108+
}
109109
}
110110
}
111111

espflash/src/image_format/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@ mod esp8266;
1818
const ESP_MAGIC: u8 = 0xE9;
1919
const WP_PIN_DISABLED: u8 = 0xEE;
2020

21+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumVariantNames)]
22+
#[strum(serialize_all = "kebab-case")]
23+
pub enum ImageFormatType {
24+
Esp8266,
25+
IdfBoot,
26+
DirectBoot,
27+
McuBoot,
28+
}
29+
30+
impl FromStr for ImageFormatType {
31+
type Err = Error;
32+
33+
fn from_str(s: &str) -> Result<Self, Self::Err> {
34+
use ImageFormatType::*;
35+
36+
match s.to_lowercase().as_str() {
37+
"esp8266" => Ok(Esp8266),
38+
"idf-boot" => Ok(IdfBoot),
39+
"direct-boot" => Ok(DirectBoot),
40+
"mcu-boot" => Ok(McuBoot),
41+
_ => Err(Error::UnknownImageFormat(s.to_string())),
42+
}
43+
}
44+
}
45+
2146
#[derive(Copy, Clone, Zeroable, Pod, Debug)]
2247
#[repr(C, packed)]
2348
struct EspCommonHeader {

0 commit comments

Comments
 (0)