Skip to content

Commit 7f41ad8

Browse files
bugadaniSergioGasquez
authored andcommitted
Return crate::Error
1 parent 50b42d5 commit 7f41ad8

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- `FlashData::new` now returns `crate::Error` (#591)
17+
1618
### Removed
1719

1820
## [3.0.0-rc.1] - 2024-02-16

espflash/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ pub enum Error {
202202

203203
#[error("Internal Error")]
204204
InternalError,
205+
206+
#[error("Failed to open file: {0}")]
207+
FileOpenError(String, #[source] std::io::Error),
208+
209+
#[error("Failed to parse partition table")]
210+
Partition(#[from] esp_idf_part::Error),
205211
}
206212

207213
#[cfg(feature = "serialport")]

espflash/src/flasher/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::{
1717
use esp_idf_part::PartitionTable;
1818
use log::{debug, info, warn};
1919
use md5::{Digest, Md5};
20-
use miette::{Context, IntoDiagnostic, Result};
2120
use serde::{Deserialize, Serialize};
2221
#[cfg(feature = "serialport")]
2322
use serialport::UsbPortInfo;
@@ -332,7 +331,7 @@ impl<'a> FlashDataBuilder<'a> {
332331
}
333332

334333
/// Builds a [`FlashData`] object.
335-
pub fn build(self) -> Result<FlashData> {
334+
pub fn build(self) -> Result<FlashData, Error> {
336335
FlashData::new(
337336
self.bootloader_path,
338337
self.partition_table_path,
@@ -364,12 +363,13 @@ impl FlashData {
364363
target_app_partition: Option<String>,
365364
flash_settings: FlashSettings,
366365
min_chip_rev: u16,
367-
) -> Result<Self> {
366+
) -> Result<Self, Error> {
368367
// If the '--bootloader' option is provided, load the binary file at the
369368
// specified path.
370369
let bootloader = if let Some(path) = bootloader {
371-
let path = fs::canonicalize(path).into_diagnostic()?;
372-
let data: Vec<u8> = fs::read(path).into_diagnostic()?;
370+
let data = fs::canonicalize(path)
371+
.and_then(fs::read)
372+
.map_err(|e| Error::FileOpenError(path.display().to_string(), e))?;
373373

374374
Some(data)
375375
} else {
@@ -1176,10 +1176,8 @@ pub(crate) fn checksum(data: &[u8], mut checksum: u8) -> u8 {
11761176
}
11771177

11781178
/// Parse a [PartitionTable] from the provided path
1179-
pub fn parse_partition_table(path: &Path) -> Result<PartitionTable> {
1180-
let data = fs::read(path)
1181-
.into_diagnostic()
1182-
.wrap_err("Failed to open partition table")?;
1179+
pub fn parse_partition_table(path: &Path) -> Result<PartitionTable, Error> {
1180+
let data = fs::read(path).map_err(|e| Error::FileOpenError(path.display().to_string(), e))?;
11831181

1184-
PartitionTable::try_from(data).into_diagnostic()
1182+
Ok(PartitionTable::try_from(data)?)
11851183
}

0 commit comments

Comments
 (0)