Skip to content

Commit 1bc03cd

Browse files
Refactor extract_comments to reduce nesting
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent 514e6c0 commit 1bc03cd

1 file changed

Lines changed: 40 additions & 30 deletions

File tree

  • tools/package-manager/src

‎tools/package-manager/src/lib.rs‎

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -410,46 +410,56 @@ pub struct DocItem {
410410
pub struct DocExtractor;
411411

412412
impl DocExtractor {
413+
fn extract_symbol_name(line: &str) -> Option<String> {
414+
let parts: Vec<&str> = line.split_whitespace().collect();
415+
if parts.len() <= 1 {
416+
return None;
417+
}
418+
419+
let sym_name = parts[1]
420+
.split('(')
421+
.next()
422+
.unwrap_or("")
423+
.split('{')
424+
.next()
425+
.unwrap_or("")
426+
.split('=')
427+
.next()
428+
.unwrap_or("")
429+
.trim();
430+
431+
if sym_name.is_empty() {
432+
None
433+
} else {
434+
Some(sym_name.to_string())
435+
}
436+
}
437+
413438
pub fn extract_comments(content: &str) -> Vec<DocItem> {
414439
let mut items = Vec::new();
415440
let mut active_doc = Vec::new();
416441

417442
for line in content.lines() {
418443
let line = line.trim();
444+
419445
if let Some(doc) = line.strip_prefix("///") {
420446
active_doc.push(doc.trim().to_string());
421-
} else if line.starts_with("function")
422-
|| line.starts_with("make")
423-
|| line.starts_with("model")
424-
|| line.starts_with("struct")
425-
{
426-
if !active_doc.is_empty() {
427-
// Extract name of symbol following keyword
428-
let parts: Vec<&str> = line.split_whitespace().collect();
429-
if parts.len() > 1 {
430-
let raw_name = parts[1];
431-
let sym_name = raw_name
432-
.split('(')
433-
.next()
434-
.unwrap_or("")
435-
.split('{')
436-
.next()
437-
.unwrap_or("")
438-
.split('=')
439-
.next()
440-
.unwrap_or("")
441-
.trim();
442-
443-
if !sym_name.is_empty() {
444-
items.push(DocItem {
445-
name: sym_name.to_string(),
446-
doc: active_doc.join("\n"),
447-
});
448-
}
447+
continue;
448+
}
449+
450+
if !active_doc.is_empty() {
451+
if line.starts_with("function")
452+
|| line.starts_with("make")
453+
|| line.starts_with("model")
454+
|| line.starts_with("struct")
455+
{
456+
if let Some(name) = Self::extract_symbol_name(line) {
457+
items.push(DocItem {
458+
name,
459+
doc: active_doc.join("\n"),
460+
});
449461
}
450-
active_doc.clear();
451462
}
452-
} else {
453463
active_doc.clear();
454464
}
455465
}

0 commit comments

Comments
 (0)