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.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 97a56a31f..9a1b12a16 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -1,6 +1,25 @@
1extern crate itertools; 1extern crate itertools;
2#[macro_use]
3extern crate failure;
4extern crate ron;
5extern crate tera;
6extern crate heck;
2 7
8use std::{
9 collections::HashMap,
10 fs,
11 path::{Path, PathBuf},
12};
3use itertools::Itertools; 13use itertools::Itertools;
14use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
15
16pub type Result<T> = ::std::result::Result<T, failure::Error>;
17
18const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
19pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs";
20pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/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";
4 23
5#[derive(Debug)] 24#[derive(Debug)]
6pub struct Test { 25pub struct Test {
@@ -41,3 +60,61 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
41 } 60 }
42 res 61 res
43} 62}
63
64
65pub fn 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
80pub fn render_template(template: &Path) -> Result<String> {
81 let grammar: ron::value::Value = {
82 let text = fs::read_to_string(project_root().join(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
118pub fn project_root() -> PathBuf {
119 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).parent().unwrap().to_path_buf()
120}