-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
a0a9aef
commit 3aee2b2
Showing
15 changed files
with
262 additions
and
425 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ use indicatif::{style::ProgressStyle, HumanCount, ProgressBar, ProgressDrawTarge | |
use log::{debug, info}; | ||
use miette::{IntoDiagnostic, Result, WrapErr}; | ||
use serialport::{SerialPortType, UsbPortInfo}; | ||
use strum::VariantNames; | ||
|
||
use self::{config::Config, monitor::monitor, serial::get_serial_port_info}; | ||
use crate::{ | ||
|
@@ -39,26 +38,6 @@ pub mod monitor; | |
|
||
mod serial; | ||
|
||
// Since as of `[email protected]` the `possible_values` attribute is no longer | ||
// present, we must use the more convoluted `value_parser` attribute instead. | ||
// Since this is a bit tedious, we'll use a helper macro to abstract away all | ||
// the cruft. It's important to note that this macro assumes the | ||
// `strum::EnumVariantNames` trait has been implemented for the provided type, | ||
// and that the provided type is in scope when calling this macro. | ||
// | ||
// See this comment for details: | ||
// https://github.com/clap-rs/clap/discussions/4264#discussioncomment-3737696 | ||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! clap_enum_variants { | ||
($e: ty) => {{ | ||
use clap::builder::TypedValueParser; | ||
clap::builder::PossibleValuesParser::new(<$e>::VARIANTS).map(|s| s.parse::<$e>().unwrap()) | ||
}}; | ||
} | ||
|
||
pub use clap_enum_variants; | ||
|
||
/// Establish a connection with a target device | ||
#[derive(Debug, Args)] | ||
pub struct ConnectArgs { | ||
|
@@ -85,13 +64,13 @@ pub struct ConnectArgs { | |
#[derive(Debug, Args)] | ||
pub struct FlashConfigArgs { | ||
/// Flash frequency | ||
#[arg(short = 'f', long, value_name = "FREQ", value_parser = clap_enum_variants!(FlashFrequency))] | ||
#[arg(short = 'f', long, value_name = "FREQ", value_enum)] | ||
pub flash_freq: Option<FlashFrequency>, | ||
/// Flash mode to use | ||
#[arg(short = 'm', long, value_name = "MODE", value_parser = clap_enum_variants!(FlashMode))] | ||
#[arg(short = 'm', long, value_name = "MODE", value_enum)] | ||
pub flash_mode: Option<FlashMode>, | ||
/// Flash size of the target | ||
#[arg(short = 's', long, value_name = "SIZE", value_parser = clap_enum_variants!(FlashSize))] | ||
#[arg(short = 's', long, value_name = "SIZE", value_enum)] | ||
pub flash_size: Option<FlashSize>, | ||
} | ||
|
||
|
@@ -111,10 +90,16 @@ pub struct FlashArgs { | |
)] | ||
pub erase_parts: Option<Vec<String>>, | ||
/// Erase specified data partitions | ||
#[arg(long, requires = "partition_table", value_name = "PARTS", value_parser = clap_enum_variants!(DataType), value_delimiter = ',')] | ||
#[arg( | ||
long, | ||
requires = "partition_table", | ||
value_name = "PARTS", | ||
value_enum, | ||
value_delimiter = ',' | ||
)] | ||
pub erase_data_parts: Option<Vec<DataType>>, | ||
/// Image format to flash | ||
#[arg(long, value_parser = clap_enum_variants!(ImageFormatKind))] | ||
#[arg(long, value_enum)] | ||
pub format: Option<ImageFormatKind>, | ||
/// Open a serial monitor after flashing | ||
#[arg(short = 'M', long)] | ||
|
@@ -155,7 +140,7 @@ pub struct SaveImageArgs { | |
#[arg(long, value_name = "FILE")] | ||
pub bootloader: Option<PathBuf>, | ||
/// Chip to create an image for | ||
#[arg(long, value_parser = clap_enum_variants!(Chip))] | ||
#[arg(long, value_enum)] | ||
pub chip: Chip, | ||
/// File name to save the generated image to | ||
pub file: PathBuf, | ||
|
@@ -193,16 +178,14 @@ where | |
_ => ProgressDrawTarget::hidden(), | ||
}; | ||
|
||
let progress = ProgressBar::with_draw_target(len, draw_target) | ||
ProgressBar::with_draw_target(len, draw_target) | ||
.with_message(msg) | ||
.with_style( | ||
ProgressStyle::default_bar() | ||
.template("[{elapsed_precise}] [{bar:40}] {pos:>7}/{len:7} {msg}") | ||
.unwrap() | ||
.progress_chars("=> "), | ||
); | ||
|
||
progress | ||
) | ||
} | ||
|
||
/// Create a callback function for the provided [ProgressBar] | ||
|
@@ -277,7 +260,7 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> { | |
|
||
/// Connect to a target device and print information about its chip | ||
pub fn board_info(args: &ConnectArgs, config: &Config) -> Result<()> { | ||
let mut flasher = connect(&args, config)?; | ||
let mut flasher = connect(args, config)?; | ||
print_board_info(&mut flasher)?; | ||
|
||
Ok(()) | ||
|
@@ -291,7 +274,7 @@ pub fn print_board_info(flasher: &mut Flasher) -> Result<()> { | |
if let Some((major, minor)) = info.revision { | ||
println!(" (revision v{major}.{minor})"); | ||
} else { | ||
println!(""); | ||
println!(); | ||
} | ||
println!("Crystal frequency: {}MHz", info.crystal_frequency); | ||
println!("Flash size: {}", info.flash_size); | ||
|
@@ -321,7 +304,7 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> { | |
// The 26MHz ESP32-C2's need to be treated as a special case. | ||
let default_baud = if chip == Chip::Esp32c2 | ||
&& !args.connect_args.use_stub | ||
&& target.crystal_freq(&mut flasher.connection())? == 26 | ||
&& target.crystal_freq(flasher.connection())? == 26 | ||
{ | ||
74_880 | ||
} else { | ||
|
@@ -537,7 +520,7 @@ pub fn erase_partitions( | |
for label in part_labels { | ||
let part = partition_table | ||
.find(label.as_str()) | ||
.ok_or(MissingPartition::from(label))?; | ||
.ok_or_else(|| MissingPartition::from(label))?; | ||
|
||
parts_to_erase | ||
.get_or_insert(HashMap::new()) | ||
|
@@ -648,12 +631,12 @@ fn pretty_print(table: PartitionTable) { | |
|
||
for p in table.partitions() { | ||
pretty.add_row(vec![ | ||
Cell::new(&p.name()).fg(Color::Green), | ||
Cell::new(&p.ty().to_string()).fg(Color::Cyan), | ||
Cell::new(&p.subtype().to_string()).fg(Color::Magenta), | ||
Cell::new(&format!("{:#x}", p.offset())).fg(Color::Red), | ||
Cell::new(&format!("{:#x} ({}KiB)", p.size(), p.size() / 1024)).fg(Color::Yellow), | ||
Cell::new(&p.encrypted()).fg(Color::DarkCyan), | ||
Cell::new(p.name()).fg(Color::Green), | ||
Cell::new(p.ty().to_string()).fg(Color::Cyan), | ||
Cell::new(p.subtype().to_string()).fg(Color::Magenta), | ||
Cell::new(format!("{:#x}", p.offset())).fg(Color::Red), | ||
Cell::new(format!("{:#x} ({}KiB)", p.size(), p.size() / 1024)).fg(Color::Yellow), | ||
Cell::new(p.encrypted()).fg(Color::DarkCyan), | ||
]); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.