Skip to content

Commit 31817fa

Browse files
bugadaniSergioGasquez
authored andcommitted
Add FlashDataBuilder
1 parent d7dc3ae commit 31817fa

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131
- Fixed a missed `flush` call that may be causing communication errors (#521)
3232

3333
### Changed
34-
- Created `FlashData` and `FlashSettings` structs to reduce number of input arguments in some functions (#512)
34+
- Created `FlashData`, `FlashDataBuilder` and `FlashSettings` structs to reduce number of input arguments in some functions (#512, #566)
3535
- `espflash` will now exit with an error if `defmt` is selected but not usable (#524)
3636

3737
### Removed

espflash/src/flasher/mod.rs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ impl FlashSettings {
272272
freq: None,
273273
}
274274
}
275+
275276
pub fn new(
276277
mode: Option<FlashMode>,
277278
size: Option<FlashSize>,
@@ -281,6 +282,93 @@ impl FlashSettings {
281282
}
282283
}
283284

285+
/// Builder interface to create [`FlashData`] objects.
286+
pub struct FlashDataBuilder<'a> {
287+
bootloader_path: Option<&'a Path>,
288+
partition_table_path: Option<&'a Path>,
289+
partition_table_offset: Option<u32>,
290+
image_format: Option<ImageFormatKind>,
291+
target_app_partition: Option<String>,
292+
flash_settings: FlashSettings,
293+
min_chip_rev: u16,
294+
}
295+
296+
impl<'a> Default for FlashDataBuilder<'a> {
297+
fn default() -> Self {
298+
Self {
299+
bootloader_path: Default::default(),
300+
partition_table_path: Default::default(),
301+
partition_table_offset: Default::default(),
302+
image_format: Default::default(),
303+
target_app_partition: Default::default(),
304+
flash_settings: FlashSettings::default(),
305+
min_chip_rev: Default::default(),
306+
}
307+
}
308+
}
309+
310+
impl<'a> FlashDataBuilder<'a> {
311+
/// Creates a new [`FlashDataBuilder`] object.
312+
pub fn new() -> Self {
313+
Self::default()
314+
}
315+
316+
/// Sets the bootloader path.
317+
pub fn with_bootloader(mut self, bootloader_path: &'a Path) -> Self {
318+
self.bootloader_path = Some(bootloader_path);
319+
self
320+
}
321+
322+
/// Sets the partition table path.
323+
pub fn with_partition_table(mut self, partition_table_path: &'a Path) -> Self {
324+
self.partition_table_path = Some(partition_table_path);
325+
self
326+
}
327+
328+
/// Sets the partition table offset.
329+
pub fn with_partition_table_offset(mut self, partition_table_offset: u32) -> Self {
330+
self.partition_table_offset = Some(partition_table_offset);
331+
self
332+
}
333+
334+
/// Sets the image format.
335+
pub fn with_image_format(mut self, image_format: ImageFormatKind) -> Self {
336+
self.image_format = Some(image_format);
337+
self
338+
}
339+
340+
/// Sets the label of the target app partition.
341+
pub fn with_target_app_partition(mut self, target_app_partition: String) -> Self {
342+
self.target_app_partition = Some(target_app_partition);
343+
self
344+
}
345+
346+
/// Sets the flash settings.
347+
pub fn with_flash_settings(mut self, flash_settings: FlashSettings) -> Self {
348+
self.flash_settings = flash_settings;
349+
self
350+
}
351+
352+
/// Sets the minimum chip revision.
353+
pub fn with_min_chip_rev(mut self, min_chip_rev: u16) -> Self {
354+
self.min_chip_rev = min_chip_rev;
355+
self
356+
}
357+
358+
/// Builds a [`FlashData`] object.
359+
pub fn build(self) -> Result<FlashData> {
360+
FlashData::new(
361+
self.bootloader_path,
362+
self.partition_table_path,
363+
self.partition_table_offset,
364+
self.image_format,
365+
self.target_app_partition,
366+
self.flash_settings,
367+
self.min_chip_rev,
368+
)
369+
}
370+
}
371+
284372
/// Flash data and configuration
285373
#[derive(Debug, Clone)]
286374
#[non_exhaustive]
@@ -308,7 +396,7 @@ impl FlashData {
308396
// specified path.
309397
let bootloader = if let Some(path) = bootloader {
310398
let path = fs::canonicalize(path).into_diagnostic()?;
311-
let data = fs::read(path).into_diagnostic()?;
399+
let data: Vec<u8> = fs::read(path).into_diagnostic()?;
312400

313401
Some(data)
314402
} else {

0 commit comments

Comments
 (0)