Skip to content

Commit 839df53

Browse files
pykzed-agent
andcommitted
build: refactor string parsing logic
The format_all_md function used if/else with string slicing, which is less idiomatic and less safe than pattern matching. This change refactors the string parsing to use match with strip_prefix, making the code more readable and maintainable. Also updates function signatures to accept the most general types needed. Changes: - Use match with strip_prefix instead of if/else and string slicing - Change save_crate_path_name parameter from &PathBuf to &Path - Remove redundant borrows where string coercion works - Simplify unwrap_or_else closure to unwrap_or - Add Path to imports for type consistency Breaking change: - None All 66 tests pass. No clippy warnings. Co-Authored-By: GLM 4.7 <253101093+zed-agent@users.noreply.github.com> Signed-off-by: pyk <2213646+pyk@users.noreply.github.com>
1 parent 814e23c commit 839df53

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/commands/build.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use log::{debug, info};
88
use scraper::{Html, Selector};
99
use std::collections::HashMap;
1010
use std::fs;
11-
use std::path::PathBuf;
11+
use std::path::{Path, PathBuf};
1212

1313
use crate::cargo;
1414
use crate::html2md;
@@ -107,11 +107,11 @@ pub fn build(crate_name: &str) -> Result<()> {
107107
let crate_dir_name = html_dir
108108
.file_name()
109109
.and_then(|n| n.to_str())
110-
.unwrap_or_else(|| crate_name);
110+
.unwrap_or(crate_name);
111111
debug!("Crate directory name (source of truth): {}", crate_dir_name);
112112

113113
// Format all.md with crate name heading and prefixed items
114-
let formatted_content = format_all_md(&crate_dir_name, &all_markdown_content);
114+
let formatted_content = format_all_md(crate_dir_name, &all_markdown_content);
115115

116116
let all_path = output_dir.join("all.md");
117117
debug!("Writing all.md to: {:?}", all_path);
@@ -122,7 +122,7 @@ pub fn build(crate_name: &str) -> Result<()> {
122122
println!("Generated markdown: {}", all_path.display());
123123

124124
info!("Extracting item mappings from all.html");
125-
let item_mappings = extract_item_mappings(&crate_dir_name, &all_html_content)?;
125+
let item_mappings = extract_item_mappings(crate_dir_name, &all_html_content)?;
126126
debug!("Found {} items to convert", item_mappings.len());
127127

128128
// Generate markdown for each item
@@ -156,7 +156,7 @@ pub fn build(crate_name: &str) -> Result<()> {
156156
info!("Generated markdown for {} items", item_mappings.len());
157157

158158
// Save crate path name for use by show and list commands
159-
save_crate_path_name(&output_dir, &crate_dir_name)?;
159+
save_crate_path_name(&output_dir, crate_dir_name)?;
160160

161161
Ok(())
162162
}
@@ -165,7 +165,7 @@ pub fn build(crate_name: &str) -> Result<()> {
165165
///
166166
/// Stores the crate directory name (source of truth from cargo doc) in
167167
/// docmd/<crate>/name for use by show and list commands.
168-
fn save_crate_path_name(output_dir: &PathBuf, path_name: &str) -> Result<()> {
168+
fn save_crate_path_name(output_dir: &Path, path_name: &str) -> Result<()> {
169169
let name_path = output_dir.join("name");
170170

171171
fs::write(&name_path, path_name)
@@ -295,17 +295,21 @@ fn format_all_md(crate_name: &str, content: &str) -> String {
295295
if line.starts_with("### ") {
296296
in_section = true;
297297
result.push(line.to_string());
298-
} else if line.starts_with("- ") {
299-
let item = &line[2..];
300-
result.push(format!("- {}::{}", crate_name, item));
301-
302-
// Collect first item from each section
303-
if in_section {
304-
first_items.push(format!("{}::{}", crate_name, item));
305-
in_section = false; // Only collect first item per section
306-
}
307298
} else {
308-
result.push(line.to_string());
299+
match line.strip_prefix("- ") {
300+
Some(item) => {
301+
result.push(format!("- {}::{}", crate_name, item));
302+
303+
// Collect first item from each section
304+
if in_section {
305+
first_items.push(format!("{}::{}", crate_name, item));
306+
in_section = false; // Only collect first item per section
307+
}
308+
}
309+
None => {
310+
result.push(line.to_string());
311+
}
312+
}
309313
}
310314
}
311315

0 commit comments

Comments
 (0)