Skip to content

Commit

Permalink
Add FlashDataBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Jan 30, 2024
1 parent d7dc3ae commit 1045ae8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, #?)
- `espflash` will now exit with an error if `defmt` is selected but not usable (#524)

### Removed
Expand Down
81 changes: 81 additions & 0 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,87 @@ 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<u32>,
image_format: Option<ImageFormatKind>,
target_app_partition: Option<String>,
flash_settings: FlashSettings,
min_chip_rev: u16,
}

impl<'a> FlashDataBuilder<'a> {
/// Creates a new [`FlashDataBuilder`] object.
pub fn new() -> Self {
FlashDataBuilder {
bootloader_path: None,
partition_table_path: None,
partition_table_offset: None,
image_format: None,
target_app_partition: None,
flash_settings: FlashSettings::default(),
min_chip_rev: 0,
}
}

/// 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> {
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]
Expand Down

0 comments on commit 1045ae8

Please sign in to comment.