Skip to content

Commit f7e3128

Browse files
authored
Clean up binary_format help text (probe-rs#3766)
1 parent 65030bc commit f7e3128

File tree

4 files changed

+49
-52
lines changed

4 files changed

+49
-52
lines changed

probe-rs-tools/src/bin/probe-rs/cmd/cargo_embed/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ async fn main_try(args: Vec<OsString>, config: Config, offset: UtcOffset) -> Res
271271
};
272272

273273
let format_options = FormatOptions::default();
274-
let format = FormatKind::from(format_options.to_format_kind(session.target()));
274+
let format = format_options.to_format_kind(session.target());
275275
let elf = if matches!(format, FormatKind::Elf | FormatKind::Idf) {
276276
Some(fs::read(&path)?)
277277
} else {

probe-rs-tools/src/bin/probe-rs/cmd/dap_server/server/session_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::{
33
core_data::{CoreData, CoreHandle},
44
};
55
use crate::{
6-
FormatKind,
76
cmd::{
87
dap_server::{
98
DebuggerError,
@@ -25,6 +24,7 @@ use anyhow::{Result, anyhow};
2524
use probe_rs::{
2625
BreakpointCause, CoreStatus, HaltReason, Session, VectorCatchCondition,
2726
config::{Registry, TargetSelector},
27+
flashing::FormatKind,
2828
probe::list::Lister,
2929
rtt::ScanRegion,
3030
};

probe-rs-tools/src/bin/probe-rs/main.rs

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::cmp::Reverse;
77
use std::collections::HashMap;
88
use std::fs;
99
use std::path::Path;
10-
use std::str::FromStr;
1110
use std::{ffi::OsString, path::PathBuf};
1211

1312
use anyhow::{Context, Result};
@@ -358,16 +357,15 @@ pub struct ElfCliOptions {
358357
#[derive(clap::Parser, Clone, Serialize, Deserialize, Debug, Default, Schema)]
359358
#[serde(default)]
360359
pub struct FormatOptions {
361-
/// If a format is provided, use it.
362-
/// If a target has a preferred format, we use that.
363-
/// Finally, if neither of the above cases are true, we default to ELF.
360+
/// The format of the firmware image.
364361
#[clap(
365362
value_enum,
366363
ignore_case = true,
364+
default_value_t = FormatKind::Target,
367365
long,
368366
help_heading = "DOWNLOAD CONFIGURATION"
369367
)]
370-
binary_format: Option<FormatKind>,
368+
binary_format: FormatKind,
371369

372370
#[clap(flatten)]
373371
bin_options: BinaryCliOptions,
@@ -380,54 +378,42 @@ pub struct FormatOptions {
380378
}
381379

382380
/// A finite list of all the available binary formats probe-rs understands.
383-
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Schema)]
381+
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, ValueEnum, Schema)]
384382
pub enum FormatKind {
385-
/// Marks a file in binary format. This means that the file contains the contents of the flash 1:1.
386-
/// [BinOptions] can be used to define the location in flash where the file contents should be put at.
387-
/// Additionally using the same config struct, you can skip the first N bytes of the binary file to have them not put into the flash.
383+
/// The image format is determined by the target chip's preference, which is usually ELF.
384+
#[default]
385+
Target,
386+
387+
/// The image is in binary format. This means that the file contains the contents of the flash 1:1.
388+
#[value(alias("binary"))]
388389
Bin,
389-
/// Marks a file in [Intel HEX](https://en.wikipedia.org/wiki/Intel_HEX) format.
390+
391+
/// The image is in Intel HEX format. For more information, see https://en.wikipedia.org/wiki/Intel_HEX
392+
#[value(aliases(["ihex", "intelhex"]))]
390393
Hex,
391-
/// Marks a file in the [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) format.
392-
#[default]
394+
395+
/// The image is in the Executable and Linkable Format (ELF). For more information, see https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
393396
Elf,
394-
/// Marks a file in the [ESP-IDF bootloader](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/app_image_format.html#app-image-structures) format.
395-
/// Use [IdfOptions] to configure flashing.
397+
398+
/// The image is an ELF file containing an ESP-IDF bootloader compatible application. For more information, see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/app_image_format.html#app-image-structures
399+
#[value(aliases(["esp-idf", "espidf"]))]
396400
Idf,
397-
/// Marks a file in the [UF2](https://github.com/microsoft/uf2) format.
401+
402+
/// The image is in the Universal Flash Storage (UF2) format. For more information, see https://github.com/microsoft/uf2
398403
Uf2,
399404
}
400405

401406
impl FormatKind {
402-
/// Creates a new Format from an optional string.
403-
///
404-
/// If the string is `None`, the default format is returned.
405-
pub fn from_optional(s: Option<&str>) -> Result<Self, String> {
406-
match s {
407-
Some(format) => Self::from_str(format),
408-
None => Ok(Self::default()),
409-
}
410-
}
411-
}
412-
413-
impl FromStr for FormatKind {
414-
type Err = String;
415-
416-
fn from_str(s: &str) -> Result<Self, Self::Err> {
417-
match &s.to_lowercase()[..] {
418-
"bin" | "binary" => Ok(Self::Bin),
419-
"hex" | "ihex" | "intelhex" => Ok(Self::Hex),
420-
"elf" => Ok(Self::Elf),
421-
"uf2" => Ok(Self::Uf2),
422-
"idf" | "esp-idf" | "espidf" => Ok(Self::Idf),
423-
_ => Err(format!("Format '{s}' is unknown.")),
424-
}
425-
}
426-
}
407+
fn to_probe_rs(self, target: &Target) -> probe_rs::flashing::FormatKind {
408+
let this = if self == FormatKind::Target {
409+
FormatKind::from_optional(target.default_format.as_deref())
410+
.expect("Failed to parse a default binary format. This shouldn't happen.")
411+
} else {
412+
self
413+
};
427414

428-
impl From<FormatKind> for probe_rs::flashing::FormatKind {
429-
fn from(kind: FormatKind) -> Self {
430-
match kind {
415+
match this {
416+
FormatKind::Target => unreachable!(),
431417
FormatKind::Bin => probe_rs::flashing::FormatKind::Bin,
432418
FormatKind::Hex => probe_rs::flashing::FormatKind::Hex,
433419
FormatKind::Elf => probe_rs::flashing::FormatKind::Elf,
@@ -437,15 +423,24 @@ impl From<FormatKind> for probe_rs::flashing::FormatKind {
437423
}
438424
}
439425

426+
impl FormatKind {
427+
/// Creates a new Format from an optional string.
428+
///
429+
/// If the string is `None`, the default format is returned.
430+
pub fn from_optional(s: Option<&str>) -> Result<Self, String> {
431+
match s {
432+
Some(format) => Self::from_str(format, true),
433+
None => Ok(Self::Elf),
434+
}
435+
}
436+
}
437+
440438
impl FormatOptions {
441439
/// If a format is provided, use it.
442440
/// If a target has a preferred format, we use that.
443441
/// Finally, if neither of the above cases are true, we default to [`Format::default()`].
444-
pub fn to_format_kind(&self, target: &Target) -> FormatKind {
445-
self.binary_format.unwrap_or_else(|| {
446-
FormatKind::from_optional(target.default_format.as_deref())
447-
.expect("Failed to parse a default binary format. This shouldn't happen.")
448-
})
442+
pub fn to_format_kind(&self, target: &Target) -> probe_rs::flashing::FormatKind {
443+
self.binary_format.to_probe_rs(target)
449444
}
450445
}
451446

probe-rs-tools/src/bin/probe-rs/util/flash.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::FormatOptions;
12
use crate::rpc::functions::flash::{FlashLayout, Operation, ProgressEvent};
2-
use crate::{FormatKind, FormatOptions};
33

44
use super::common_options::{BinaryDownloadOptions, LoadedProbeOptions, OperationError};
55
use super::logging;
@@ -13,7 +13,9 @@ use colored::Colorize;
1313
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
1414
use parking_lot::Mutex;
1515
use probe_rs::InstructionSet;
16-
use probe_rs::flashing::{BinOptions, ElfOptions, FlashError, FlashProgress, Format, IdfOptions};
16+
use probe_rs::flashing::{
17+
BinOptions, ElfOptions, FlashError, FlashProgress, Format, FormatKind, IdfOptions,
18+
};
1719
use probe_rs::{
1820
Session,
1921
flashing::{DownloadOptions, FileDownloadError, FlashLoader},

0 commit comments

Comments
 (0)