minore: improve readability
Tuples are hard to understand
This commit is contained in:
parent
c45792ae8f
commit
80991356e1
1 changed files with 32 additions and 40 deletions
|
@ -43,6 +43,7 @@ pub fn list_files(dir: &Path) -> Vec<PathBuf> {
|
|||
res
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CommentBlock {
|
||||
pub id: String,
|
||||
pub line: usize,
|
||||
|
@ -55,11 +56,10 @@ impl CommentBlock {
|
|||
|
||||
let tag = format!("{}:", tag);
|
||||
let mut res = Vec::new();
|
||||
for (line, mut block) in do_extract_comment_blocks(text, true) {
|
||||
let first = block.remove(0);
|
||||
for mut block in CommentBlock::do_extract(text, true) {
|
||||
let first = block.contents.remove(0);
|
||||
if let Some(id) = first.strip_prefix(&tag) {
|
||||
let id = id.trim().to_string();
|
||||
let block = CommentBlock { id, line, contents: block };
|
||||
block.id = id.trim().to_string();
|
||||
res.push(block);
|
||||
}
|
||||
}
|
||||
|
@ -67,48 +67,40 @@ impl CommentBlock {
|
|||
}
|
||||
|
||||
pub fn extract_untagged(text: &str) -> Vec<CommentBlock> {
|
||||
CommentBlock::do_extract(text, false)
|
||||
}
|
||||
|
||||
fn do_extract(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<CommentBlock> {
|
||||
let mut res = Vec::new();
|
||||
for (line, block) in do_extract_comment_blocks(text, false) {
|
||||
let id = String::new();
|
||||
let block = CommentBlock { id, line, contents: block };
|
||||
res.push(block);
|
||||
|
||||
let prefix = "// ";
|
||||
let lines = text.lines().map(str::trim_start);
|
||||
|
||||
let dummy_block = CommentBlock { id: String::new(), line: 0, contents: Vec::new() };
|
||||
let mut block = dummy_block.clone();
|
||||
for (line_num, line) in lines.enumerate() {
|
||||
if line == "//" && allow_blocks_with_empty_lines {
|
||||
block.contents.push(String::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let is_comment = line.starts_with(prefix);
|
||||
if is_comment {
|
||||
block.contents.push(line[prefix.len()..].to_string());
|
||||
} else {
|
||||
if !block.contents.is_empty() {
|
||||
res.push(mem::replace(&mut block, dummy_block.clone()));
|
||||
}
|
||||
block.line = line_num + 2;
|
||||
}
|
||||
}
|
||||
if !block.contents.is_empty() {
|
||||
res.push(block)
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn do_extract_comment_blocks(
|
||||
text: &str,
|
||||
allow_blocks_with_empty_lines: bool,
|
||||
) -> Vec<(usize, Vec<String>)> {
|
||||
let mut res = Vec::new();
|
||||
|
||||
let prefix = "// ";
|
||||
let lines = text.lines().map(str::trim_start);
|
||||
|
||||
let mut block = (0, vec![]);
|
||||
for (line_num, line) in lines.enumerate() {
|
||||
if line == "//" && allow_blocks_with_empty_lines {
|
||||
block.1.push(String::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let is_comment = line.starts_with(prefix);
|
||||
if is_comment {
|
||||
block.1.push(line[prefix.len()..].to_string());
|
||||
} else {
|
||||
if !block.1.is_empty() {
|
||||
res.push(mem::take(&mut block));
|
||||
}
|
||||
block.0 = line_num + 2;
|
||||
}
|
||||
}
|
||||
if !block.1.is_empty() {
|
||||
res.push(block)
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Location {
|
||||
pub file: PathBuf,
|
||||
|
|
Loading…
Add table
Reference in a new issue