Skip to content

Commit 3aee2b2

Browse files
authored
CLI improvements and dependency updates (#334)
* Update dependencies to their latest versions * Derive `clap::ValueEnum` instead of using a macro * Simplify flash config enums, make them more consistent * Fix a bunch of clippy warnings
1 parent a0a9aef commit 3aee2b2

File tree

15 files changed

+262
-425
lines changed

15 files changed

+262
-425
lines changed

Cargo.lock

Lines changed: 136 additions & 216 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-espflash/Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ pkg-fmt = "zip"
3131
[dependencies]
3232
cargo = { version = "0.66.0", features = ["vendored-openssl"] }
3333
cargo_metadata = "0.15.2"
34-
clap = { version = "4.0.29", features = ["derive"] }
34+
clap = { version = "4.0.32", features = ["derive"] }
3535
env_logger = "0.10.0"
3636
esp-idf-part = "0.1.2"
3737
espflash = { version = "=2.0.0-rc.2", path = "../espflash" }
3838
log = "0.4.17"
3939
miette = { version = "5.5.0", features = ["fancy"] }
40-
serde = { version = "1.0.148", features = ["derive"] }
41-
strum = "0.24.1"
42-
thiserror = "1.0.37"
43-
toml = "0.5.9"
40+
serde = { version = "1.0.152", features = ["derive"] }
41+
thiserror = "1.0.38"
42+
toml = "0.5.10"

cargo-espflash/src/main.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use cargo_metadata::Message;
88
use clap::{Args, Parser, Subcommand};
99
use espflash::{
1010
cli::{
11-
self, board_info, clap_enum_variants, config::Config, connect, erase_partitions,
12-
flash_elf_image, monitor::monitor, parse_partition_table, partition_table,
13-
print_board_info, save_elf_as_image, serial_monitor, ConnectArgs, FlashConfigArgs,
14-
MonitorArgs, PartitionTableArgs,
11+
self, board_info, config::Config, connect, erase_partitions, flash_elf_image,
12+
monitor::monitor, parse_partition_table, partition_table, print_board_info,
13+
save_elf_as_image, serial_monitor, ConnectArgs, FlashConfigArgs, MonitorArgs,
14+
PartitionTableArgs,
1515
},
1616
image_format::ImageFormatKind,
1717
logging::initialize_logger,
@@ -20,7 +20,6 @@ use espflash::{
2020
};
2121
use log::{debug, LevelFilter};
2222
use miette::{IntoDiagnostic, Result, WrapErr};
23-
use strum::VariantNames;
2423

2524
use crate::{
2625
cargo_config::CargoConfig,
@@ -110,7 +109,7 @@ struct FlashArgs {
110109
#[derive(Debug, Args)]
111110
struct SaveImageArgs {
112111
/// Image format to flash
113-
#[arg(long, value_parser = clap_enum_variants!(ImageFormatKind))]
112+
#[arg(long, value_enum)]
114113
pub format: Option<ImageFormatKind>,
115114

116115
#[clap(flatten)]
@@ -163,7 +162,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
163162

164163
let chip = flasher.chip();
165164
let target = chip.into_target();
166-
let target_xtal_freq = target.crystal_freq(&mut flasher.connection())?;
165+
let target_xtal_freq = target.crystal_freq(flasher.connection())?;
167166

168167
let build_ctx =
169168
build(&args.build_args, &cargo_config, chip).wrap_err("Failed to build project")?;
@@ -321,7 +320,7 @@ fn build(
321320
let output = Command::new("cargo")
322321
.arg("build")
323322
.args(args)
324-
.args(&["--message-format", "json-diagnostic-rendered-ansi"])
323+
.args(["--message-format", "json-diagnostic-rendered-ansi"])
325324
.stdout(Stdio::piped())
326325
.stderr(Stdio::inherit())
327326
.spawn()

espflash/Cargo.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ required-features = ["cli"]
3636

3737
[dependencies]
3838
addr2line = { version = "0.19.0", optional = true }
39-
base64 = "0.13.1"
39+
base64 = "0.21.0"
4040
binread = "2.2.0"
4141
bytemuck = { version = "1.12.3", features = ["derive"] }
42-
clap = { version = "4.0.29", features = ["derive", "env"], optional = true }
43-
comfy-table = { version = "6.1.3", optional = true }
42+
clap = { version = "4.0.32", features = ["derive", "env"], optional = true }
43+
comfy-table = { version = "6.1.4", optional = true }
4444
crossterm = { version = "0.25.0", optional = true }
4545
dialoguer = { version = "0.10.2", optional = true }
4646
directories-next = { version = "2.0.0", optional = true }
@@ -52,18 +52,18 @@ lazy_static = { version = "1.4.0", optional = true }
5252
log = "0.4.17"
5353
miette = { version = "5.5.0", features = ["fancy"] }
5454
parse_int = { version = "0.6.0", optional = true }
55-
regex = { version = "1.7.0", optional = true }
55+
regex = { version = "1.7.1", optional = true }
5656
rppal = { version = "0.14.1", optional = true }
57-
serde = { version = "1.0.148", features = ["derive"] }
57+
serde = { version = "1.0.152", features = ["derive"] }
5858
serde-hex = { version = "0.1.0", optional = true }
5959
serialport = "4.2.0"
6060
sha2 = "0.10.6"
6161
slip-codec = "0.3.3"
6262
strum = { version = "0.24.1", features = ["derive"] }
63-
thiserror = "1.0.37"
64-
toml = "0.5.9"
65-
update-informer = { version = "0.5.0", optional = true }
66-
xmas-elf = "0.8.0"
63+
thiserror = "1.0.38"
64+
toml = "0.5.10"
65+
update-informer = { version = "0.6.0", optional = true }
66+
xmas-elf = "0.9.0"
6767

6868
[features]
6969
default = ["cli"]

espflash/src/bin/espflash.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use std::{
88
use clap::{Args, Parser, Subcommand};
99
use espflash::{
1010
cli::{
11-
self, board_info, build_progress_bar_callback, clap_enum_variants, config::Config, connect,
12-
erase_partitions, flash_elf_image, monitor::monitor, parse_partition_table,
13-
partition_table, print_board_info, progress_bar, save_elf_as_image, serial_monitor,
14-
ConnectArgs, FlashConfigArgs, MonitorArgs, PartitionTableArgs,
11+
self, board_info, build_progress_bar_callback, config::Config, connect, erase_partitions,
12+
flash_elf_image, monitor::monitor, parse_partition_table, partition_table,
13+
print_board_info, progress_bar, save_elf_as_image, serial_monitor, ConnectArgs,
14+
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
1515
},
1616
image_format::ImageFormatKind,
1717
logging::initialize_logger,
@@ -20,7 +20,6 @@ use espflash::{
2020
};
2121
use log::{debug, LevelFilter};
2222
use miette::{IntoDiagnostic, Result, WrapErr};
23-
use strum::VariantNames;
2423

2524
#[derive(Debug, Parser)]
2625
#[clap(about, version, propagate_version = true)]
@@ -57,7 +56,7 @@ struct FlashArgs {
5756
#[derive(Debug, Args)]
5857
struct SaveImageArgs {
5958
/// Image format to flash
60-
#[arg(long, value_parser = clap_enum_variants!(ImageFormatKind))]
59+
#[arg(long, value_enum)]
6160
format: Option<ImageFormatKind>,
6261

6362
#[clap(flatten)]
@@ -121,7 +120,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
121120

122121
let chip = flasher.chip();
123122
let target = chip.into_target();
124-
let target_xtal_freq = target.crystal_freq(&mut flasher.connection())?;
123+
let target_xtal_freq = target.crystal_freq(flasher.connection())?;
125124

126125
// Read the ELF data from the build path and load it to the target.
127126
let elf_data = fs::read(&args.image).into_diagnostic()?;

espflash/src/cli/mod.rs

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use indicatif::{style::ProgressStyle, HumanCount, ProgressBar, ProgressDrawTarge
2222
use log::{debug, info};
2323
use miette::{IntoDiagnostic, Result, WrapErr};
2424
use serialport::{SerialPortType, UsbPortInfo};
25-
use strum::VariantNames;
2625

2726
use self::{config::Config, monitor::monitor, serial::get_serial_port_info};
2827
use crate::{
@@ -39,26 +38,6 @@ pub mod monitor;
3938

4039
mod serial;
4140

42-
// Since as of `[email protected]` the `possible_values` attribute is no longer
43-
// present, we must use the more convoluted `value_parser` attribute instead.
44-
// Since this is a bit tedious, we'll use a helper macro to abstract away all
45-
// the cruft. It's important to note that this macro assumes the
46-
// `strum::EnumVariantNames` trait has been implemented for the provided type,
47-
// and that the provided type is in scope when calling this macro.
48-
//
49-
// See this comment for details:
50-
// https://github.com/clap-rs/clap/discussions/4264#discussioncomment-3737696
51-
#[doc(hidden)]
52-
#[macro_export]
53-
macro_rules! clap_enum_variants {
54-
($e: ty) => {{
55-
use clap::builder::TypedValueParser;
56-
clap::builder::PossibleValuesParser::new(<$e>::VARIANTS).map(|s| s.parse::<$e>().unwrap())
57-
}};
58-
}
59-
60-
pub use clap_enum_variants;
61-
6241
/// Establish a connection with a target device
6342
#[derive(Debug, Args)]
6443
pub struct ConnectArgs {
@@ -85,13 +64,13 @@ pub struct ConnectArgs {
8564
#[derive(Debug, Args)]
8665
pub struct FlashConfigArgs {
8766
/// Flash frequency
88-
#[arg(short = 'f', long, value_name = "FREQ", value_parser = clap_enum_variants!(FlashFrequency))]
67+
#[arg(short = 'f', long, value_name = "FREQ", value_enum)]
8968
pub flash_freq: Option<FlashFrequency>,
9069
/// Flash mode to use
91-
#[arg(short = 'm', long, value_name = "MODE", value_parser = clap_enum_variants!(FlashMode))]
70+
#[arg(short = 'm', long, value_name = "MODE", value_enum)]
9271
pub flash_mode: Option<FlashMode>,
9372
/// Flash size of the target
94-
#[arg(short = 's', long, value_name = "SIZE", value_parser = clap_enum_variants!(FlashSize))]
73+
#[arg(short = 's', long, value_name = "SIZE", value_enum)]
9574
pub flash_size: Option<FlashSize>,
9675
}
9776

@@ -111,10 +90,16 @@ pub struct FlashArgs {
11190
)]
11291
pub erase_parts: Option<Vec<String>>,
11392
/// Erase specified data partitions
114-
#[arg(long, requires = "partition_table", value_name = "PARTS", value_parser = clap_enum_variants!(DataType), value_delimiter = ',')]
93+
#[arg(
94+
long,
95+
requires = "partition_table",
96+
value_name = "PARTS",
97+
value_enum,
98+
value_delimiter = ','
99+
)]
115100
pub erase_data_parts: Option<Vec<DataType>>,
116101
/// Image format to flash
117-
#[arg(long, value_parser = clap_enum_variants!(ImageFormatKind))]
102+
#[arg(long, value_enum)]
118103
pub format: Option<ImageFormatKind>,
119104
/// Open a serial monitor after flashing
120105
#[arg(short = 'M', long)]
@@ -155,7 +140,7 @@ pub struct SaveImageArgs {
155140
#[arg(long, value_name = "FILE")]
156141
pub bootloader: Option<PathBuf>,
157142
/// Chip to create an image for
158-
#[arg(long, value_parser = clap_enum_variants!(Chip))]
143+
#[arg(long, value_enum)]
159144
pub chip: Chip,
160145
/// File name to save the generated image to
161146
pub file: PathBuf,
@@ -193,16 +178,14 @@ where
193178
_ => ProgressDrawTarget::hidden(),
194179
};
195180

196-
let progress = ProgressBar::with_draw_target(len, draw_target)
181+
ProgressBar::with_draw_target(len, draw_target)
197182
.with_message(msg)
198183
.with_style(
199184
ProgressStyle::default_bar()
200185
.template("[{elapsed_precise}] [{bar:40}] {pos:>7}/{len:7} {msg}")
201186
.unwrap()
202187
.progress_chars("=> "),
203-
);
204-
205-
progress
188+
)
206189
}
207190

208191
/// Create a callback function for the provided [ProgressBar]
@@ -277,7 +260,7 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
277260

278261
/// Connect to a target device and print information about its chip
279262
pub fn board_info(args: &ConnectArgs, config: &Config) -> Result<()> {
280-
let mut flasher = connect(&args, config)?;
263+
let mut flasher = connect(args, config)?;
281264
print_board_info(&mut flasher)?;
282265

283266
Ok(())
@@ -291,7 +274,7 @@ pub fn print_board_info(flasher: &mut Flasher) -> Result<()> {
291274
if let Some((major, minor)) = info.revision {
292275
println!(" (revision v{major}.{minor})");
293276
} else {
294-
println!("");
277+
println!();
295278
}
296279
println!("Crystal frequency: {}MHz", info.crystal_frequency);
297280
println!("Flash size: {}", info.flash_size);
@@ -321,7 +304,7 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
321304
// The 26MHz ESP32-C2's need to be treated as a special case.
322305
let default_baud = if chip == Chip::Esp32c2
323306
&& !args.connect_args.use_stub
324-
&& target.crystal_freq(&mut flasher.connection())? == 26
307+
&& target.crystal_freq(flasher.connection())? == 26
325308
{
326309
74_880
327310
} else {
@@ -537,7 +520,7 @@ pub fn erase_partitions(
537520
for label in part_labels {
538521
let part = partition_table
539522
.find(label.as_str())
540-
.ok_or(MissingPartition::from(label))?;
523+
.ok_or_else(|| MissingPartition::from(label))?;
541524

542525
parts_to_erase
543526
.get_or_insert(HashMap::new())
@@ -648,12 +631,12 @@ fn pretty_print(table: PartitionTable) {
648631

649632
for p in table.partitions() {
650633
pretty.add_row(vec![
651-
Cell::new(&p.name()).fg(Color::Green),
652-
Cell::new(&p.ty().to_string()).fg(Color::Cyan),
653-
Cell::new(&p.subtype().to_string()).fg(Color::Magenta),
654-
Cell::new(&format!("{:#x}", p.offset())).fg(Color::Red),
655-
Cell::new(&format!("{:#x} ({}KiB)", p.size(), p.size() / 1024)).fg(Color::Yellow),
656-
Cell::new(&p.encrypted()).fg(Color::DarkCyan),
634+
Cell::new(p.name()).fg(Color::Green),
635+
Cell::new(p.ty().to_string()).fg(Color::Cyan),
636+
Cell::new(p.subtype().to_string()).fg(Color::Magenta),
637+
Cell::new(format!("{:#x}", p.offset())).fg(Color::Red),
638+
Cell::new(format!("{:#x} ({}KiB)", p.size(), p.size() / 1024)).fg(Color::Yellow),
639+
Cell::new(p.encrypted()).fg(Color::DarkCyan),
657640
]);
658641
}
659642

espflash/src/cli/serial.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ pub fn get_serial_port_info(
3333
let ports = detect_usb_serial_ports().unwrap_or_default();
3434

3535
if let Some(serial) = &matches.port {
36-
find_serial_port(&ports, &serial)
36+
find_serial_port(&ports, serial)
3737
} else if let Some(serial) = &config.connection.serial {
38-
find_serial_port(&ports, &serial)
38+
find_serial_port(&ports, serial)
3939
} else {
4040
let (port, matches) = select_serial_port(ports, config)?;
4141

espflash/src/command.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<'a> Command<'a> {
205205
data_command(writer, data, pad_to, pad_byte, sequence)?;
206206
}
207207
Command::FlashEnd { reboot } => {
208-
write_basic(writer, &[if reboot { 0 } else { 1 }], 0)?;
208+
write_basic(writer, &[u8::from(!reboot)], 0)?;
209209
}
210210
Command::MemBegin {
211211
size,
@@ -242,7 +242,7 @@ impl<'a> Command<'a> {
242242
entry: u32,
243243
}
244244
let params = EntryParams {
245-
no_entry: if reboot { 1 } else { 0 },
245+
no_entry: u32::from(reboot),
246246
entry,
247247
};
248248
write_basic(writer, bytes_of(&params), 0)?;
@@ -325,7 +325,7 @@ impl<'a> Command<'a> {
325325
data_command(writer, data, pad_to, pad_byte, sequence)?;
326326
}
327327
Command::FlashDeflateEnd { reboot } => {
328-
write_basic(writer, &[if reboot { 0 } else { 1 }], 0)?;
328+
write_basic(writer, &[u8::from(!reboot)], 0)?;
329329
}
330330
Command::FlashDetect => {
331331
write_basic(writer, &[], 0)?;

0 commit comments

Comments
 (0)