aboutsummaryrefslogtreecommitdiff
path: root/crates/tools/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/tools/src/lib.rs')
-rw-r--r--crates/tools/src/lib.rs85
1 files changed, 22 insertions, 63 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 5d5d372bb..444745be5 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -1,25 +1,20 @@
1extern crate itertools; 1extern crate itertools;
2#[macro_use]
3extern crate failure; 2extern crate failure;
4extern crate heck; 3extern crate teraron;
5extern crate ron;
6extern crate tera;
7 4
8use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
9use itertools::Itertools;
10use std::{ 5use std::{
11 collections::HashMap,
12 fs,
13 path::{Path, PathBuf}, 6 path::{Path, PathBuf},
14}; 7};
15 8
9use itertools::Itertools;
10
11pub use teraron::{Mode, Verify, Overwrite};
12
16pub type Result<T> = ::std::result::Result<T, failure::Error>; 13pub type Result<T> = ::std::result::Result<T, failure::Error>;
17 14
18const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; 15pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
19pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs"; 16pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera";
20pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; 17pub const AST: &str = "ra_syntax/src/ast/generated.rs.tera";
21pub const AST: &str = "ra_syntax/src/ast/generated.rs";
22pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera";
23 18
24#[derive(Debug)] 19#[derive(Debug)]
25pub struct Test { 20pub struct Test {
@@ -61,59 +56,23 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
61 res 56 res
62} 57}
63 58
64pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { 59pub fn generate(mode: Mode) -> Result<()> {
65 match fs::read_to_string(path) { 60 let grammar = project_root().join(GRAMMAR);
66 Ok(ref old_contents) if old_contents == contents => { 61 let syntax_kinds = project_root().join(SYNTAX_KINDS);
67 return Ok(()); 62 let ast = project_root().join(AST);
68 } 63 teraron::generate(
69 _ => (), 64 &syntax_kinds,
70 } 65 &grammar,
71 if verify { 66 mode,
72 bail!("`{}` is not up-to-date", path.display()); 67 )?;
73 } 68 teraron::generate(
74 eprintln!("updating {}", path.display()); 69 &ast,
75 fs::write(path, contents)?; 70 &grammar,
71 mode,
72 )?;
76 Ok(()) 73 Ok(())
77} 74}
78 75
79pub fn render_template(template: &Path) -> Result<String> {
80 let grammar: ron::value::Value = {
81 let text = fs::read_to_string(project_root().join(GRAMMAR))?;
82 ron::de::from_str(&text)?
83 };
84 let template = fs::read_to_string(template)?;
85 let mut tera = tera::Tera::default();
86 tera.add_raw_template("grammar", &template)
87 .map_err(|e| format_err!("template error: {:?}", e))?;
88 tera.register_function("concat", Box::new(concat));
89 tera.register_filter("camel", |arg, _| {
90 Ok(arg.as_str().unwrap().to_camel_case().into())
91 });
92 tera.register_filter("snake", |arg, _| {
93 Ok(arg.as_str().unwrap().to_snake_case().into())
94 });
95 tera.register_filter("SCREAM", |arg, _| {
96 Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
97 });
98 let ret = tera
99 .render("grammar", &grammar)
100 .map_err(|e| format_err!("template error: {:?}", e))?;
101 return Ok(ret);
102
103 fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
104 let mut elements = Vec::new();
105 for &key in ["a", "b", "c"].iter() {
106 let val = match args.get(key) {
107 Some(val) => val,
108 None => continue,
109 };
110 let val = val.as_array().unwrap();
111 elements.extend(val.iter().cloned());
112 }
113 Ok(tera::Value::Array(elements))
114 }
115}
116
117pub fn project_root() -> PathBuf { 76pub fn project_root() -> PathBuf {
118 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) 77 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
119 .parent() 78 .parent()