From 7673c6ea6818d8e557bc05de00e5d4d17f1feb7d Mon Sep 17 00:00:00 2001 From: Jack Dyre <83248599+JackDyre@users.noreply.github.com> Date: Wed, 19 Feb 2025 23:42:38 -0600 Subject: [PATCH] fix panics from `clap` internal type casting --- src/cli.rs | 33 ++++++++++++--------------------- src/cli_utils.rs | 4 ++-- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 5243cb032..1e2c67a98 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -256,8 +256,8 @@ impl Cli { }); let num_format_style: NumberFormatStyle = matches - .get_one::("num_format_style") - .cloned() + .get_one::("num_format_style") + .map(parse_or_exit) .unwrap_or_default(); let number_format = match num_format_style.get_format() { @@ -268,30 +268,21 @@ impl Cli { } }; - // Sorting category should be restricted by clap but parse before we do - // work just in case. - let (sort, sort_reverse) = if let Some(sort) = matches.get_one::("sort") { - (Some(sort.clone()), false) - } else { - let sort = matches.get_one::("rsort"); - (sort.cloned(), sort.is_some()) - }; - let sort = sort.map(|x| match Sort::from_str(&x) { - Ok(sort) => sort, - Err(e) => { - eprintln!("Error:\n{}", e); - process::exit(1); - } - }); + let mut sort_reverse = false; + let sort = matches.get_one::("sort"); + let rsort = matches.get_one::("rsort"); + + if rsort.is_some() { + sort_reverse = true; + } + + let sort = sort.or(rsort).map(parse_or_exit); // Format category is overly accepting by clap (so the user knows what // is supported) but this will fail if support is not compiled in and // give a useful error to the user. let output = matches.get_one("output").cloned(); - let streaming = matches - .get_one("streaming") - .cloned() - .map(parse_or_exit::); + let streaming = matches.get_one::("streaming").map(parse_or_exit); crate::cli_utils::setup_logger(verbose); diff --git a/src/cli_utils.rs b/src/cli_utils.rs index 9bedce940..4d4c936a9 100644 --- a/src/cli_utils.rs +++ b/src/cli_utils.rs @@ -54,12 +54,12 @@ pub fn setup_logger(verbose_option: u64) { builder.init(); } -pub fn parse_or_exit(s: &str) -> T +pub fn parse_or_exit(s: impl AsRef) -> T where T: FromStr, T::Err: fmt::Display, { - T::from_str(s).unwrap_or_else(|e| { + T::from_str(s.as_ref()).unwrap_or_else(|e| { eprintln!("Error:\n{}", e); process::exit(1); })