Skip to content

Commit 6842ed9

Browse files
committed
refactor(complete): Remove low-value w macro
1 parent 17d6d24 commit 6842ed9

File tree

6 files changed

+36
-48
lines changed

6 files changed

+36
-48
lines changed

clap_complete/src/macros.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
macro_rules! w {
2-
($buf:expr, $to_w:expr) => {
3-
match $buf.write_all($to_w) {
4-
Ok(..) => (),
5-
Err(..) => panic!("Failed to write to generated file"),
6-
}
7-
};
8-
}
9-
101
#[cfg(feature = "debug")]
112
macro_rules! debug {
123
($($arg:tt)*) => {

clap_complete/src/shells/bash.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ impl Generator for Bash {
2020

2121
let fn_name = bin_name.replace('-', "__");
2222

23-
w!(
23+
write!(
2424
buf,
25-
format!(
26-
"_{name}() {{
25+
"_{name}() {{
2726
local i cur prev opts cmd
2827
COMPREPLY=()
2928
cur=\"${{COMP_WORDS[COMP_CWORD]}}\"
@@ -66,15 +65,13 @@ else
6665
complete -F _{name} -o bashdefault -o default {name}
6766
fi
6867
",
69-
name = bin_name,
70-
cmd = fn_name,
71-
name_opts = all_options_for_path(cmd, bin_name),
72-
name_opts_details = option_details_for_path(cmd, bin_name),
73-
subcmds = all_subcommands(cmd, &fn_name),
74-
subcmd_details = subcommand_details(cmd)
75-
)
76-
.as_bytes()
77-
);
68+
name = bin_name,
69+
cmd = fn_name,
70+
name_opts = all_options_for_path(cmd, bin_name),
71+
name_opts_details = option_details_for_path(cmd, bin_name),
72+
subcmds = all_subcommands(cmd, &fn_name),
73+
subcmd_details = subcommand_details(cmd)
74+
).expect("failed to write completion file");
7875
}
7976
}
8077

@@ -274,22 +271,23 @@ fn all_options_for_path(cmd: &Command, path: &str) -> String {
274271

275272
let mut opts = String::new();
276273
for short in utils::shorts_and_visible_aliases(p) {
277-
write!(&mut opts, "-{short} ").unwrap();
274+
write!(&mut opts, "-{short} ").expect("writing to String is infallible");
278275
}
279276
for long in utils::longs_and_visible_aliases(p) {
280-
write!(&mut opts, "--{long} ").unwrap();
277+
write!(&mut opts, "--{long} ").expect("writing to String is infallible");
281278
}
282279
for pos in p.get_positionals() {
283280
if let Some(vals) = utils::possible_values(pos) {
284281
for value in vals {
285-
write!(&mut opts, "{} ", value.get_name()).unwrap();
282+
write!(&mut opts, "{} ", value.get_name())
283+
.expect("writing to String is infallible");
286284
}
287285
} else {
288-
write!(&mut opts, "{pos} ").unwrap();
286+
write!(&mut opts, "{pos} ").expect("writing to String is infallible");
289287
}
290288
}
291289
for (sc, _) in utils::subcommands(p) {
292-
write!(&mut opts, "{sc} ").unwrap();
290+
write!(&mut opts, "{sc} ").expect("writing to String is infallible");
293291
}
294292
opts.pop();
295293

clap_complete/src/shells/elvish.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ impl Generator for Elvish {
2222

2323
let subcommands_cases = generate_inner(cmd, "");
2424

25-
let result = format!(
25+
write!(
26+
buf,
2627
r#"
2728
use builtin;
2829
use str;
@@ -46,9 +47,8 @@ set edit:completion:arg-completer[{bin_name}] = {{|@words|
4647
$completions[$command]
4748
}}
4849
"#,
49-
);
50-
51-
w!(buf, result.as_bytes());
50+
)
51+
.expect("failed to write completion file");
5252
}
5353
}
5454

clap_complete/src/shells/fish.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Generator for Fish {
4242
needs_fn_name,
4343
using_fn_name,
4444
);
45-
w!(buf, buffer.as_bytes());
45+
write!(buf, "{buffer}").expect("failed to write completion file");
4646
}
4747
}
4848

@@ -240,7 +240,9 @@ fn gen_subcommand_helpers(
240240
}
241241
}
242242
let optspecs_fn_name = format!("__fish_{bin_name}_global_optspecs");
243-
let template = format!("\
243+
write!(
244+
buf,
245+
"\
244246
# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.\n\
245247
function {optspecs_fn_name}\n\
246248
\tstring join \\n{optspecs}\n\
@@ -264,8 +266,7 @@ fn gen_subcommand_helpers(
264266
\tand return 1\n\
265267
\tcontains -- $cmd[1] $argv\n\
266268
end\n\n\
267-
");
268-
w!(buf, template.as_bytes());
269+
").expect("failed to write completion file");
269270
}
270271

271272
fn value_completion(option: &Arg) -> String {

clap_complete/src/shells/powershell.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ impl Generator for PowerShell {
2222

2323
let subcommands_cases = generate_inner(cmd, "");
2424

25-
let result = format!(
25+
write!(
26+
buf,
2627
r#"
2728
using namespace System.Management.Automation
2829
using namespace System.Management.Automation.Language
@@ -51,9 +52,8 @@ Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{
5152
Sort-Object -Property ListItemText
5253
}}
5354
"#
54-
);
55-
56-
w!(buf, result.as_bytes());
55+
)
56+
.expect("failed to write completion file");
5757
}
5858
}
5959

clap_complete/src/shells/zsh.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ impl Generator for Zsh {
1919
.get_bin_name()
2020
.expect("crate::generate should have set the bin_name");
2121

22-
w!(
22+
write!(
2323
buf,
24-
format!(
25-
"#compdef {name}
24+
"#compdef {name}
2625
2726
autoload -U is-at-least
2827
@@ -49,13 +48,12 @@ else
4948
compdef _{name} {name}
5049
fi
5150
",
52-
name = bin_name,
53-
initial_args = get_args_of(cmd, None),
54-
subcommands = get_subcommands_of(cmd),
55-
subcommand_details = subcommand_details(cmd)
56-
)
57-
.as_bytes()
58-
);
51+
name = bin_name,
52+
initial_args = get_args_of(cmd, None),
53+
subcommands = get_subcommands_of(cmd),
54+
subcommand_details = subcommand_details(cmd)
55+
)
56+
.expect("failed to write completion file");
5957
}
6058
}
6159

0 commit comments

Comments
 (0)