Skip to content

Commit

Permalink
Add FlashDataBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani authored and SergioGasquez committed Jan 31, 2024
1 parent d7dc3ae commit 31817fa
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
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, #566)
- `espflash` will now exit with an error if `defmt` is selected but not usable (#524)

### Removed
Expand Down
90 changes: 89 additions & 1 deletion espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl FlashSettings {
freq: None,
}
}

pub fn new(
mode: Option<FlashMode>,
size: Option<FlashSize>,
Expand All @@ -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<u32>,
image_format: Option<ImageFormatKind>,
target_app_partition: Option<String>,
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> {
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 Expand Up @@ -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<u8> = fs::read(path).into_diagnostic()?;

Some(data)
} else {
Expand Down

0 comments on commit 31817fa

Please sign in to comment.