aboutsummaryrefslogtreecommitdiff
path: root/crates/tools/src/main.rs
diff options
context:
space:
mode:
authorMuhammad Mominul Huque <[email protected]>2018-10-14 15:32:57 +0100
committerMuhammad Mominul Huque <[email protected]>2018-10-14 15:32:57 +0100
commit114e9a2d74f1fa43c3418f8cd978fd4186fcb78b (patch)
tree922ec7c131d19594b37c0185f894a696f3e577e7 /crates/tools/src/main.rs
parent2ba6f18586d02a6dbc32e0bea88f7b4236277ea1 (diff)
create cli.rs and make the tests passing
Diffstat (limited to 'crates/tools/src/main.rs')
-rw-r--r--crates/tools/src/main.rs62
1 files changed, 3 insertions, 59 deletions
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index ee900553c..cf5e662b0 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -1,21 +1,17 @@
1extern crate clap; 1extern crate clap;
2#[macro_use] 2#[macro_use]
3extern crate failure; 3extern crate failure;
4extern crate ron;
5extern crate tera;
6extern crate tools; 4extern crate tools;
7extern crate walkdir; 5extern crate walkdir;
8extern crate heck;
9 6
10use clap::{App, Arg, SubCommand}; 7use clap::{App, Arg, SubCommand};
11use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
12use std::{ 8use std::{
13 collections::HashMap, 9 collections::HashMap,
14 fs, 10 fs,
15 path::{Path, PathBuf}, 11 path::{Path, PathBuf},
16 process::Command, 12 process::Command,
17}; 13};
18use tools::{collect_tests, Test}; 14use tools::{Test, collect_tests, render_template, update};
19 15
20type Result<T> = ::std::result::Result<T, failure::Error>; 16type Result<T> = ::std::result::Result<T, failure::Error>;
21 17
@@ -51,8 +47,8 @@ fn main() -> Result<()> {
51fn run_gen_command(name: &str, verify: bool) -> Result<()> { 47fn run_gen_command(name: &str, verify: bool) -> Result<()> {
52 match name { 48 match name {
53 "gen-kinds" => { 49 "gen-kinds" => {
54 update(Path::new(SYNTAX_KINDS), &render_template(SYNTAX_KINDS_TEMPLATE)?, verify)?; 50 update(Path::new(SYNTAX_KINDS), &render_template(SYNTAX_KINDS_TEMPLATE, GRAMMAR)?, verify)?;
55 update(Path::new(AST), &render_template(AST_TEMPLATE)?, verify)?; 51 update(Path::new(AST), &render_template(AST_TEMPLATE, GRAMMAR)?, verify)?;
56 }, 52 },
57 "gen-tests" => { 53 "gen-tests" => {
58 gen_tests(verify)? 54 gen_tests(verify)?
@@ -62,58 +58,6 @@ fn run_gen_command(name: &str, verify: bool) -> Result<()> {
62 Ok(()) 58 Ok(())
63} 59}
64 60
65fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
66 match fs::read_to_string(path) {
67 Ok(ref old_contents) if old_contents == contents => {
68 return Ok(());
69 }
70 _ => (),
71 }
72 if verify {
73 bail!("`{}` is not up-to-date", path.display());
74 }
75 eprintln!("updating {}", path.display());
76 fs::write(path, contents)?;
77 Ok(())
78}
79
80fn render_template(template: &str) -> Result<String> {
81 let grammar: ron::value::Value = {
82 let text = fs::read_to_string(GRAMMAR)?;
83 ron::de::from_str(&text)?
84 };
85 let template = fs::read_to_string(template)?;
86 let mut tera = tera::Tera::default();
87 tera.add_raw_template("grammar", &template)
88 .map_err(|e| format_err!("template error: {:?}", e))?;
89 tera.register_function("concat", Box::new(concat));
90 tera.register_filter("camel", |arg, _| {
91 Ok(arg.as_str().unwrap().to_camel_case().into())
92 });
93 tera.register_filter("snake", |arg, _| {
94 Ok(arg.as_str().unwrap().to_snake_case().into())
95 });
96 tera.register_filter("SCREAM", |arg, _| {
97 Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
98 });
99 let ret = tera
100 .render("grammar", &grammar)
101 .map_err(|e| format_err!("template error: {:?}", e))?;
102 return Ok(ret);
103
104 fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
105 let mut elements = Vec::new();
106 for &key in ["a", "b", "c"].iter() {
107 let val = match args.get(key) {
108 Some(val) => val,
109 None => continue,
110 };
111 let val = val.as_array().unwrap();
112 elements.extend(val.iter().cloned());
113 }
114 Ok(tera::Value::Array(elements))
115 }
116}
117 61
118fn gen_tests(verify: bool) -> Result<()> { 62fn gen_tests(verify: bool) -> Result<()> {
119 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; 63 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?;