From c926083a4d3aba3eda4f659cb8f646f0197389b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 30 Jan 2024 18:10:55 +0100 Subject: [PATCH] Add FlashDataBuilder --- CHANGELOG.md | 2 +- espflash/src/flasher/mod.rs | 90 ++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d6fb184..6a97ef2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed a missed `flush` call that may be causing communication errors (#521) ### Changed -- Created `FlashData` and `FlashSettings` structs to reduce number of input arguments in some functions (#512) +- Created `FlashData`, `FlashDataBuilder` and `FlashSettings` structs to reduce number of input arguments in some functions (#512, #566) - `espflash` will now exit with an error if `defmt` is selected but not usable (#524) ### Removed diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 1ad8c36d..fd46f6ce 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -272,6 +272,7 @@ impl FlashSettings { freq: None, } } + pub fn new( mode: Option, size: Option, @@ -281,6 +282,93 @@ impl FlashSettings { } } +/// Builder interface to create [`FlashData`] objects. +pub struct FlashDataBuilder<'a> { + bootloader_path: Option<&'a Path>, + partition_table_path: Option<&'a Path>, + partition_table_offset: Option, + image_format: Option, + target_app_partition: Option, + flash_settings: FlashSettings, + min_chip_rev: u16, +} + +impl<'a> Default for FlashDataBuilder<'a> { + fn default() -> Self { + Self { + bootloader_path: Default::default(), + partition_table_path: Default::default(), + partition_table_offset: Default::default(), + image_format: Default::default(), + target_app_partition: Default::default(), + flash_settings: FlashSettings::default(), + min_chip_rev: Default::default(), + } + } +} + +impl<'a> FlashDataBuilder<'a> { + /// Creates a new [`FlashDataBuilder`] object. + pub fn new() -> Self { + Self::default() + } + + /// Sets the bootloader path. + pub fn with_bootloader(mut self, bootloader_path: &'a Path) -> Self { + self.bootloader_path = Some(bootloader_path); + self + } + + /// Sets the partition table path. + pub fn with_partition_table(mut self, partition_table_path: &'a Path) -> Self { + self.partition_table_path = Some(partition_table_path); + self + } + + /// Sets the partition table offset. + pub fn with_partition_table_offset(mut self, partition_table_offset: u32) -> Self { + self.partition_table_offset = Some(partition_table_offset); + self + } + + /// Sets the image format. + pub fn with_image_format(mut self, image_format: ImageFormatKind) -> Self { + self.image_format = Some(image_format); + self + } + + /// Sets the label of the target app partition. + pub fn with_target_app_partition(mut self, target_app_partition: String) -> Self { + self.target_app_partition = Some(target_app_partition); + self + } + + /// Sets the flash settings. + pub fn with_flash_settings(mut self, flash_settings: FlashSettings) -> Self { + self.flash_settings = flash_settings; + self + } + + /// Sets the minimum chip revision. + pub fn with_min_chip_rev(mut self, min_chip_rev: u16) -> Self { + self.min_chip_rev = min_chip_rev; + self + } + + /// Builds a [`FlashData`] object. + pub fn build(self) -> Result { + FlashData::new( + self.bootloader_path, + self.partition_table_path, + self.partition_table_offset, + self.image_format, + self.target_app_partition, + self.flash_settings, + self.min_chip_rev, + ) + } +} + /// Flash data and configuration #[derive(Debug, Clone)] #[non_exhaustive] @@ -308,7 +396,7 @@ impl FlashData { // specified path. let bootloader = if let Some(path) = bootloader { let path = fs::canonicalize(path).into_diagnostic()?; - let data = fs::read(path).into_diagnostic()?; + let data: Vec = fs::read(path).into_diagnostic()?; Some(data) } else {