aboutsummaryrefslogtreecommitdiff
path: root/crates/tools/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-16 18:28:43 +0100
committerAleksey Kladov <[email protected]>2018-10-16 18:31:30 +0100
commit514aa3cf853c3c4beef8f827c12328d626f977de (patch)
tree2a10336c3825ad821960c5169b78df17f7bd5f5c /crates/tools/src/lib.rs
parent1216878f7be20dd0e652fb8cdc395009fdcfae07 (diff)
extract teraron
Diffstat (limited to 'crates/tools/src/lib.rs')
-rw-r--r--crates/tools/src/lib.rs50
1 files changed, 3 insertions, 47 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 5d5d372bb..63ede5315 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -1,25 +1,18 @@
1extern crate itertools; 1extern crate itertools;
2#[macro_use] 2#[macro_use]
3extern crate failure; 3extern crate failure;
4extern crate heck;
5extern crate ron;
6extern crate tera;
7 4
8use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
9use itertools::Itertools; 5use itertools::Itertools;
10use std::{ 6use std::{
11 collections::HashMap,
12 fs, 7 fs,
13 path::{Path, PathBuf}, 8 path::{Path, PathBuf},
14}; 9};
15 10
16pub type Result<T> = ::std::result::Result<T, failure::Error>; 11pub type Result<T> = ::std::result::Result<T, failure::Error>;
17 12
18const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; 13pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
19pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs"; 14pub 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"; 15pub 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 16
24#[derive(Debug)] 17#[derive(Debug)]
25pub struct Test { 18pub struct Test {
@@ -76,43 +69,6 @@ pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
76 Ok(()) 69 Ok(())
77} 70}
78 71
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 72
117pub fn project_root() -> PathBuf { 73pub fn project_root() -> PathBuf {
118 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) 74 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())