From 26ea53e38e586e91f29a5857ca977a2d31c1810a Mon Sep 17 00:00:00 2001 From: Vincent Ging Ho Yim Date: Sat, 10 May 2025 22:42:51 +1000 Subject: [PATCH 1/6] refactor: Use `writeln!()` instead of `write!()` with explicit newline --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 207cd95..91af819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,7 +172,7 @@ fn write_help_markdown( build_table_of_contents_markdown(buffer, Vec::new(), command, 0) .unwrap(); - write!(buffer, "\n").unwrap(); + writeln!(buffer).unwrap(); } //---------------------------------------- @@ -391,7 +391,7 @@ fn build_command_markdown( writeln!(buffer, "* `{title_name}` — {about}",)?; } - write!(buffer, "\n")?; + writeln!(buffer)?; } //---------------------------------- @@ -405,7 +405,7 @@ fn build_command_markdown( write_arg_markdown(buffer, pos_arg)?; } - write!(buffer, "\n")?; + writeln!(buffer)?; } //---------------------------------- @@ -424,7 +424,7 @@ fn build_command_markdown( write_arg_markdown(buffer, arg)?; } - write!(buffer, "\n")?; + writeln!(buffer)?; } //---------------------------------- @@ -433,7 +433,7 @@ fn build_command_markdown( // Include extra space between commands. This is purely for the benefit of // anyone reading the source .md file. - write!(buffer, "\n\n")?; + writeln!(buffer, "\n")?; for subcommand in command.get_subcommands() { build_command_markdown( From 001fa24d0b8de80d97997381ef359769bc22e5be Mon Sep 17 00:00:00 2001 From: Vincent Ging Ho Yim Date: Sat, 10 May 2025 22:49:20 +1000 Subject: [PATCH 2/6] refactor: Use `push()` instead of `push_str()` with a single character --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 91af819..ae81439 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -342,7 +342,7 @@ fn build_command_markdown( String::new() } else { let mut s = parent_command_path.join(" "); - s.push_str(" "); + s.push(' '); s }, command From 2b7bb394f6d1eef0a80afba1f43ffdad840388a8 Mon Sep 17 00:00:00 2001 From: Vincent Ging Ho Yim Date: Mon, 12 May 2025 11:54:49 +1000 Subject: [PATCH 3/6] refactor: Create owned `String` in one place after unwrapping --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ae81439..4d6be93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -601,8 +601,8 @@ fn get_canonical_name(command: &clap::Command) -> String { command .get_display_name() .or_else(|| command.get_bin_name()) - .map(|name| name.to_owned()) - .unwrap_or_else(|| command.get_name().to_owned()) + .unwrap_or_else(|| command.get_name()) + .to_owned() } /// Indents non-empty lines. The output always ends with a newline. From a38483360668bb8539a7641d13cc3dfe1e5b92fc Mon Sep 17 00:00:00 2001 From: Vincent Ging Ho Yim Date: Mon, 12 May 2025 12:12:05 +1000 Subject: [PATCH 4/6] refactor: Collect into a `String` directly Also removed the extraneous turbofish. --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4d6be93..ba6a604 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -568,8 +568,7 @@ fn write_arg_markdown(buffer: &mut String, arg: &clap::Arg) -> fmt::Result { }, None => format!(" - `{}`\n", pv.get_name()), }) - .collect::>() - .join(""); + .collect(); writeln!(buffer, "\n Possible values:\n{text}")?; } else { From 4083c417b410623ed1172aa02ccf3db766ba7093 Mon Sep 17 00:00:00 2001 From: Vincent Ging Ho Yim Date: Mon, 12 May 2025 12:18:33 +1000 Subject: [PATCH 5/6] refactor: Pass parameter by reference in `build_{table_of_contents,command}_markdown()` We can just take a reference to `parent_command_path` without consuming it and create an owned copy inside the function instead. --- src/lib.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ba6a604..9eb1e79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,8 +169,7 @@ fn write_help_markdown( if options.show_table_of_contents { writeln!(buffer, "**Command Overview:**\n").unwrap(); - build_table_of_contents_markdown(buffer, Vec::new(), command, 0) - .unwrap(); + build_table_of_contents_markdown(buffer, &[], command, 0).unwrap(); writeln!(buffer).unwrap(); } @@ -179,7 +178,7 @@ fn write_help_markdown( // Write the commands/subcommands sections //---------------------------------------- - build_command_markdown(buffer, Vec::new(), command, 0, options).unwrap(); + build_command_markdown(buffer, &[], command, 0, options).unwrap(); //----------------- // Write the footer @@ -198,7 +197,7 @@ fn write_help_markdown( fn build_table_of_contents_markdown( buffer: &mut String, // Parent commands of `command`. - parent_command_path: Vec, + parent_command_path: &[String], command: &clap::Command, depth: usize, ) -> std::fmt::Result { @@ -212,7 +211,7 @@ fn build_table_of_contents_markdown( // Append the name of `command` to `command_path`. let command_path = { - let mut command_path = parent_command_path; + let mut command_path = parent_command_path.to_owned(); command_path.push(title_name); command_path }; @@ -231,7 +230,7 @@ fn build_table_of_contents_markdown( for subcommand in command.get_subcommands() { build_table_of_contents_markdown( buffer, - command_path.clone(), + &command_path, subcommand, depth + 1, )?; @@ -288,7 +287,7 @@ fn build_table_of_contents_html( fn build_command_markdown( buffer: &mut String, // Parent commands of `command`. - parent_command_path: Vec, + parent_command_path: &[String], command: &clap::Command, depth: usize, options: &MarkdownOptions, @@ -303,7 +302,7 @@ fn build_command_markdown( // Append the name of `command` to `command_path`. let command_path = { - let mut command_path = parent_command_path.clone(); + let mut command_path = parent_command_path.to_owned(); command_path.push(title_name); command_path }; @@ -438,7 +437,7 @@ fn build_command_markdown( for subcommand in command.get_subcommands() { build_command_markdown( buffer, - command_path.clone(), + &command_path, subcommand, depth + 1, options, From f8136da4b7588a40f3703838bcc22a8073c688a7 Mon Sep 17 00:00:00 2001 From: Vincent Ging Ho Yim Date: Mon, 12 May 2025 12:29:40 +1000 Subject: [PATCH 6/6] refactor: Specify explicit type for `default()` for clarity --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9eb1e79..2a1ed63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,7 +103,7 @@ pub fn help_markdown_custom( /// Format the help information for `command` as Markdown. pub fn help_markdown_command(command: &clap::Command) -> String { - return help_markdown_command_custom(command, &Default::default()); + return help_markdown_command_custom(command, &MarkdownOptions::default()); } /// Format the help information for `command` as Markdown, with custom options. @@ -130,7 +130,7 @@ pub fn print_help_markdown() { let mut buffer = String::with_capacity(100); - write_help_markdown(&mut buffer, &command, &Default::default()); + write_help_markdown(&mut buffer, &command, &MarkdownOptions::default()); println!("{}", buffer); }