diff options
author | Aleksey Kladov <[email protected]> | 2018-10-16 18:28:43 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-10-16 18:31:30 +0100 |
commit | 514aa3cf853c3c4beef8f827c12328d626f977de (patch) | |
tree | 2a10336c3825ad821960c5169b78df17f7bd5f5c /crates/tools/src/lib.rs | |
parent | 1216878f7be20dd0e652fb8cdc395009fdcfae07 (diff) |
extract teraron
Diffstat (limited to 'crates/tools/src/lib.rs')
-rw-r--r-- | crates/tools/src/lib.rs | 50 |
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 @@ | |||
1 | extern crate itertools; | 1 | extern crate itertools; |
2 | #[macro_use] | 2 | #[macro_use] |
3 | extern crate failure; | 3 | extern crate failure; |
4 | extern crate heck; | ||
5 | extern crate ron; | ||
6 | extern crate tera; | ||
7 | 4 | ||
8 | use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; | ||
9 | use itertools::Itertools; | 5 | use itertools::Itertools; |
10 | use std::{ | 6 | use std::{ |
11 | collections::HashMap, | ||
12 | fs, | 7 | fs, |
13 | path::{Path, PathBuf}, | 8 | path::{Path, PathBuf}, |
14 | }; | 9 | }; |
15 | 10 | ||
16 | pub type Result<T> = ::std::result::Result<T, failure::Error>; | 11 | pub type Result<T> = ::std::result::Result<T, failure::Error>; |
17 | 12 | ||
18 | const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; | 13 | pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; |
19 | pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs"; | 14 | pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; |
20 | pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; | 15 | pub const AST: &str = "ra_syntax/src/ast/generated.rs.tera"; |
21 | pub const AST: &str = "ra_syntax/src/ast/generated.rs"; | ||
22 | pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera"; | ||
23 | 16 | ||
24 | #[derive(Debug)] | 17 | #[derive(Debug)] |
25 | pub struct Test { | 18 | pub struct Test { |
@@ -76,43 +69,6 @@ pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { | |||
76 | Ok(()) | 69 | Ok(()) |
77 | } | 70 | } |
78 | 71 | ||
79 | pub 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 | ||
117 | pub fn project_root() -> PathBuf { | 73 | pub fn project_root() -> PathBuf { |
118 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) | 74 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) |