From c9a8302c57be631aa66023de2093a597e946c650 Mon Sep 17 00:00:00 2001 From: AudaciousAxiom <179637270+AudaciousAxiom@users.noreply.github.com> Date: Sun, 5 Jan 2025 13:47:19 +0100 Subject: [PATCH] refactor: simplify the feature documentation generation This makes the default feature label more explicit, and factors out some formatting operations to only use one `write!` statement, making the generation easier to maintain and expand. --- lib.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/lib.rs b/lib.rs index 9afbf8a..3fc9886 100644 --- a/lib.rs +++ b/lib.rs @@ -503,27 +503,22 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result { let mut result = String::new(); for (f, top, comment) in features { let default = if default_features.contains(f) { " *(enabled by default)*" } else { "" }; - if !comment.trim().is_empty() { - if let Some(feature_label) = &args.feature_label { - writeln!( - result, - "{}* {}{} —{}", - top, - feature_label.replace("{feature}", f), - default, - comment.trim_end(), - ) - .unwrap(); - } else { - writeln!(result, "{}* **`{}`**{} —{}", top, f, default, comment.trim_end()) - .unwrap(); - } - } else if let Some(feature_label) = &args.feature_label { - writeln!(result, "{}* {}{}", top, feature_label.replace("{feature}", f), default,) - .unwrap(); + let feature_label = args.feature_label.as_deref().unwrap_or("**`{feature}`**"); + let comment = if comment.trim().is_empty() { + String::new() } else { - writeln!(result, "{}* **`{}`**{}", top, f, default).unwrap(); - } + format!(" —{}", comment.trim_end()) + }; + + writeln!( + result, + "{}* {}{}{}", + top, + feature_label.replace("{feature}", f), + default, + comment, + ) + .unwrap(); } result += &top_comment; Ok(result)