diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-24 17:30:15 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-24 17:30:15 +0100 |
commit | a3a10c59e00906265ab177042948621540067f3b (patch) | |
tree | dbbb4a6c5a762661cde00d5e7b1650ab984d885a /xtask/src/codegen.rs | |
parent | 81677c3232fc4e6a9fb52889b4e82a8afdee9c7e (diff) | |
parent | a40d02c9eb1c7226bc7db87b014dc827e77f2a08 (diff) |
Merge #2062
2062: refactor comment extraction from tasks r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r-- | xtask/src/codegen.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 948b86719..bf3a90119 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs | |||
@@ -8,7 +8,7 @@ | |||
8 | mod gen_syntax; | 8 | mod gen_syntax; |
9 | mod gen_parser_tests; | 9 | mod gen_parser_tests; |
10 | 10 | ||
11 | use std::{fs, path::Path}; | 11 | use std::{fs, mem, path::Path}; |
12 | 12 | ||
13 | use crate::Result; | 13 | use crate::Result; |
14 | 14 | ||
@@ -44,3 +44,26 @@ pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { | |||
44 | fs::write(path, contents)?; | 44 | fs::write(path, contents)?; |
45 | Ok(()) | 45 | Ok(()) |
46 | } | 46 | } |
47 | |||
48 | fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { | ||
49 | let mut res = Vec::new(); | ||
50 | |||
51 | let prefix = "// "; | ||
52 | let lines = text.lines().map(str::trim_start); | ||
53 | |||
54 | let mut block = vec![]; | ||
55 | for line in lines { | ||
56 | let is_comment = line.starts_with(prefix); | ||
57 | if is_comment { | ||
58 | block.push(line[prefix.len()..].to_string()); | ||
59 | } else { | ||
60 | if !block.is_empty() { | ||
61 | res.push(mem::replace(&mut block, Vec::new())) | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | if !block.is_empty() { | ||
66 | res.push(mem::replace(&mut block, Vec::new())) | ||
67 | } | ||
68 | res | ||
69 | } | ||