From 6842ed96da9d7307e340ac82bc2d6fd27a591f9d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 16 Aug 2024 10:10:26 -0500 Subject: [PATCH] refactor(complete): Remove low-value w macro --- clap_complete/src/macros.rs | 9 -------- clap_complete/src/shells/bash.rs | 32 ++++++++++++-------------- clap_complete/src/shells/elvish.rs | 8 +++---- clap_complete/src/shells/fish.rs | 9 ++++---- clap_complete/src/shells/powershell.rs | 8 +++---- clap_complete/src/shells/zsh.rs | 18 +++++++-------- 6 files changed, 36 insertions(+), 48 deletions(-) diff --git a/clap_complete/src/macros.rs b/clap_complete/src/macros.rs index bc697946..f9e529d9 100644 --- a/clap_complete/src/macros.rs +++ b/clap_complete/src/macros.rs @@ -1,12 +1,3 @@ -macro_rules! w { - ($buf:expr, $to_w:expr) => { - match $buf.write_all($to_w) { - Ok(..) => (), - Err(..) => panic!("Failed to write to generated file"), - } - }; -} - #[cfg(feature = "debug")] macro_rules! debug { ($($arg:tt)*) => { diff --git a/clap_complete/src/shells/bash.rs b/clap_complete/src/shells/bash.rs index a8b8fb1d..5da32d14 100644 --- a/clap_complete/src/shells/bash.rs +++ b/clap_complete/src/shells/bash.rs @@ -20,10 +20,9 @@ impl Generator for Bash { let fn_name = bin_name.replace('-', "__"); - w!( + write!( buf, - format!( - "_{name}() {{ + "_{name}() {{ local i cur prev opts cmd COMPREPLY=() cur=\"${{COMP_WORDS[COMP_CWORD]}}\" @@ -66,15 +65,13 @@ else complete -F _{name} -o bashdefault -o default {name} fi ", - name = bin_name, - cmd = fn_name, - name_opts = all_options_for_path(cmd, bin_name), - name_opts_details = option_details_for_path(cmd, bin_name), - subcmds = all_subcommands(cmd, &fn_name), - subcmd_details = subcommand_details(cmd) - ) - .as_bytes() - ); + name = bin_name, + cmd = fn_name, + name_opts = all_options_for_path(cmd, bin_name), + name_opts_details = option_details_for_path(cmd, bin_name), + subcmds = all_subcommands(cmd, &fn_name), + subcmd_details = subcommand_details(cmd) + ).expect("failed to write completion file"); } } @@ -274,22 +271,23 @@ fn all_options_for_path(cmd: &Command, path: &str) -> String { let mut opts = String::new(); for short in utils::shorts_and_visible_aliases(p) { - write!(&mut opts, "-{short} ").unwrap(); + write!(&mut opts, "-{short} ").expect("writing to String is infallible"); } for long in utils::longs_and_visible_aliases(p) { - write!(&mut opts, "--{long} ").unwrap(); + write!(&mut opts, "--{long} ").expect("writing to String is infallible"); } for pos in p.get_positionals() { if let Some(vals) = utils::possible_values(pos) { for value in vals { - write!(&mut opts, "{} ", value.get_name()).unwrap(); + write!(&mut opts, "{} ", value.get_name()) + .expect("writing to String is infallible"); } } else { - write!(&mut opts, "{pos} ").unwrap(); + write!(&mut opts, "{pos} ").expect("writing to String is infallible"); } } for (sc, _) in utils::subcommands(p) { - write!(&mut opts, "{sc} ").unwrap(); + write!(&mut opts, "{sc} ").expect("writing to String is infallible"); } opts.pop(); diff --git a/clap_complete/src/shells/elvish.rs b/clap_complete/src/shells/elvish.rs index eb81e78b..b41f035e 100644 --- a/clap_complete/src/shells/elvish.rs +++ b/clap_complete/src/shells/elvish.rs @@ -22,7 +22,8 @@ impl Generator for Elvish { let subcommands_cases = generate_inner(cmd, ""); - let result = format!( + write!( + buf, r#" use builtin; use str; @@ -46,9 +47,8 @@ set edit:completion:arg-completer[{bin_name}] = {{|@words| $completions[$command] }} "#, - ); - - w!(buf, result.as_bytes()); + ) + .expect("failed to write completion file"); } } diff --git a/clap_complete/src/shells/fish.rs b/clap_complete/src/shells/fish.rs index 25404c6b..a87dd1d5 100644 --- a/clap_complete/src/shells/fish.rs +++ b/clap_complete/src/shells/fish.rs @@ -42,7 +42,7 @@ impl Generator for Fish { needs_fn_name, using_fn_name, ); - w!(buf, buffer.as_bytes()); + write!(buf, "{buffer}").expect("failed to write completion file"); } } @@ -240,7 +240,9 @@ fn gen_subcommand_helpers( } } let optspecs_fn_name = format!("__fish_{bin_name}_global_optspecs"); - let template = format!("\ + write!( + buf, + "\ # Print an optspec for argparse to handle cmd's options that are independent of any subcommand.\n\ function {optspecs_fn_name}\n\ \tstring join \\n{optspecs}\n\ @@ -264,8 +266,7 @@ fn gen_subcommand_helpers( \tand return 1\n\ \tcontains -- $cmd[1] $argv\n\ end\n\n\ - "); - w!(buf, template.as_bytes()); + ").expect("failed to write completion file"); } fn value_completion(option: &Arg) -> String { diff --git a/clap_complete/src/shells/powershell.rs b/clap_complete/src/shells/powershell.rs index efc78aba..0b718657 100644 --- a/clap_complete/src/shells/powershell.rs +++ b/clap_complete/src/shells/powershell.rs @@ -22,7 +22,8 @@ impl Generator for PowerShell { let subcommands_cases = generate_inner(cmd, ""); - let result = format!( + write!( + buf, r#" using namespace System.Management.Automation using namespace System.Management.Automation.Language @@ -51,9 +52,8 @@ Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{ Sort-Object -Property ListItemText }} "# - ); - - w!(buf, result.as_bytes()); + ) + .expect("failed to write completion file"); } } diff --git a/clap_complete/src/shells/zsh.rs b/clap_complete/src/shells/zsh.rs index 51048130..6b0e2dc1 100644 --- a/clap_complete/src/shells/zsh.rs +++ b/clap_complete/src/shells/zsh.rs @@ -19,10 +19,9 @@ impl Generator for Zsh { .get_bin_name() .expect("crate::generate should have set the bin_name"); - w!( + write!( buf, - format!( - "#compdef {name} + "#compdef {name} autoload -U is-at-least @@ -49,13 +48,12 @@ else compdef _{name} {name} fi ", - name = bin_name, - initial_args = get_args_of(cmd, None), - subcommands = get_subcommands_of(cmd), - subcommand_details = subcommand_details(cmd) - ) - .as_bytes() - ); + name = bin_name, + initial_args = get_args_of(cmd, None), + subcommands = get_subcommands_of(cmd), + subcommand_details = subcommand_details(cmd) + ) + .expect("failed to write completion file"); } }