Skip to content

Commit

Permalink
refactor: simplify the feature documentation generation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AudaciousAxiom authored and ogoffart committed Jan 6, 2025
1 parent 5237eed commit 31ffdab
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,27 +503,22 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result<String, String> {
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)
Expand Down

0 comments on commit 31ffdab

Please sign in to comment.