Skip to content

Commit

Permalink
refactor+docs: move & reword esp32c3 specific err
Browse files Browse the repository at this point in the history
This change rewords the error message from "revision 3" to "revision 3
(v0.3)" in an effort to clarify the mapping between hardware revisions
and major/minor version numbers.

Additionally, it moves the esp32c3-specific error closer to the actual
version check to make it easier to keep both in sync.
  • Loading branch information
sethp committed Mar 1, 2023
1 parent 491d86a commit c183dfa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
22 changes: 13 additions & 9 deletions espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ pub struct UnsupportedImageFormatError {
format: ImageFormatKind,
chip: Chip,
revision: Option<(u32, u32)>,
context: Option<String>,
}

impl UnsupportedImageFormatError {
Expand All @@ -409,6 +410,7 @@ impl UnsupportedImageFormatError {
format,
chip,
revision,
context: None,
}
}

Expand All @@ -421,6 +423,12 @@ impl UnsupportedImageFormatError {
.collect::<Vec<&'static str>>()
.join(", ")
}

pub fn with_context(mut self, ctx: String) -> Self {
self.context.replace(ctx);

self
}
}

impl std::error::Error for UnsupportedImageFormatError {}
Expand All @@ -447,19 +455,15 @@ impl Diagnostic for UnsupportedImageFormatError {
}

fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
let str = if self.chip == Chip::Esp32c3 && self.format == ImageFormatKind::DirectBoot {
format!(
"The {} only supports direct-boot starting with revision 3",
self.chip,
)
if let Some(ref ctx) = self.context {
Some(Box::new(ctx))
} else {
format!(
Some(Box::new(format!(
"The following image formats are supported by the {}: {}",
self.chip,
self.supported_formats()
)
};
Some(Box::new(str))
)))
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion espflash/src/targets/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ impl Target for Esp32c3 {
Ok(Box::new(DirectBootFormat::new(image, 0)?))
}
_ => Err(
UnsupportedImageFormatError::new(image_format, Chip::Esp32c3, chip_revision).into(),
UnsupportedImageFormatError::new(image_format, Chip::Esp32c3, chip_revision)
.with_context(format!(
"The {} only supports direct-boot starting with revision 3 (v0.3)",
Chip::Esp32c3,
))
.into(),
),
}
}
Expand Down

0 comments on commit c183dfa

Please sign in to comment.