Unnamed repository; edit this file 'description' to name the repository.
minore: improve readability
Tuples are hard to understand
Aleksey Kladov 2021-09-13
parent c45792a · commit 8099135
-rw-r--r--crates/sourcegen/src/lib.rs64
1 files changed, 28 insertions, 36 deletions
diff --git a/crates/sourcegen/src/lib.rs b/crates/sourcegen/src/lib.rs
index 2d28066342..7d179e00e7 100644
--- a/crates/sourcegen/src/lib.rs
+++ b/crates/sourcegen/src/lib.rs
@@ -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,46 +67,38 @@ impl CommentBlock {
}
pub fn extract_untagged(text: &str) -> 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);
- }
- res
+ CommentBlock::do_extract(text, false)
}
-}
-fn do_extract_comment_blocks(
- text: &str,
- allow_blocks_with_empty_lines: bool,
-) -> Vec<(usize, Vec<String>)> {
- let mut res = Vec::new();
+ fn do_extract(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<CommentBlock> {
+ let mut res = Vec::new();
- let prefix = "// ";
- let lines = text.lines().map(str::trim_start);
+ 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 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.1.push(line[prefix.len()..].to_string());
- } else {
- if !block.1.is_empty() {
- res.push(mem::take(&mut block));
+ 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;
}
- block.0 = line_num + 2;
}
+ if !block.contents.is_empty() {
+ res.push(block)
+ }
+ res
}
- if !block.1.is_empty() {
- res.push(block)
- }
- res
}
#[derive(Debug)]