aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-30 23:33:37 +0100
committerAleksey Kladov <[email protected]>2020-05-30 23:33:37 +0100
commit383247a9ae8202f20ce6f01d1429c1cd2a11d516 (patch)
treed1861a269d940a9369f26a95021eafaad62d66de
parente1829d8959d9f48302fec42d64216c158db13744 (diff)
Generalize
-rw-r--r--xtask/src/codegen.rs20
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs10
2 files changed, 21 insertions, 9 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index b4907f4b2..2e8fd3494 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -61,8 +61,24 @@ fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
61 do_extract_comment_blocks(text, false) 61 do_extract_comment_blocks(text, false)
62} 62}
63 63
64fn extract_comment_blocks_with_empty_lines(text: &str) -> Vec<Vec<String>> { 64fn extract_comment_blocks_with_empty_lines(tag: &str, text: &str) -> Vec<CommentBlock> {
65 do_extract_comment_blocks(text, true) 65 assert!(tag.starts_with(char::is_uppercase));
66 let tag = format!("{}:", tag);
67 let mut res = Vec::new();
68 for mut block in do_extract_comment_blocks(text, true) {
69 let first = block.remove(0);
70 if first.starts_with(&tag) {
71 let id = first[tag.len()..].trim().to_string();
72 let block = CommentBlock { id, contents: block };
73 res.push(block);
74 }
75 }
76 res
77}
78
79struct CommentBlock {
80 id: String,
81 contents: Vec<String>,
66} 82}
67 83
68fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<Vec<String>> { 84fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lines: bool) -> Vec<Vec<String>> {
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
index 20dcde820..6ebeb8aea 100644
--- a/xtask/src/codegen/gen_assists_docs.rs
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -33,22 +33,18 @@ impl Assist {
33 33
34 fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> { 34 fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> {
35 let text = fs::read_to_string(path)?; 35 let text = fs::read_to_string(path)?;
36 let comment_blocks = extract_comment_blocks_with_empty_lines(&text); 36 let comment_blocks = extract_comment_blocks_with_empty_lines("Assist", &text);
37 37
38 for block in comment_blocks { 38 for block in comment_blocks {
39 // FIXME: doesn't support blank lines yet, need to tweak 39 // FIXME: doesn't support blank lines yet, need to tweak
40 // `extract_comment_blocks` for that. 40 // `extract_comment_blocks` for that.
41 let mut lines = block.iter(); 41 let id = block.id;
42 let first_line = lines.next().unwrap();
43 if !first_line.starts_with("Assist: ") {
44 continue;
45 }
46 let id = first_line["Assist: ".len()..].to_string();
47 assert!( 42 assert!(
48 id.chars().all(|it| it.is_ascii_lowercase() || it == '_'), 43 id.chars().all(|it| it.is_ascii_lowercase() || it == '_'),
49 "invalid assist id: {:?}", 44 "invalid assist id: {:?}",
50 id 45 id
51 ); 46 );
47 let mut lines = block.contents.iter();
52 48
53 let doc = take_until(lines.by_ref(), "```").trim().to_string(); 49 let doc = take_until(lines.by_ref(), "```").trim().to_string();
54 assert!( 50 assert!(