mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
extract teraron
This commit is contained in:
parent
1216878f7b
commit
514aa3cf85
6 changed files with 164 additions and 79 deletions
|
@ -1,25 +1,18 @@
|
|||
extern crate itertools;
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
extern crate heck;
|
||||
extern crate ron;
|
||||
extern crate tera;
|
||||
|
||||
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
|
||||
use itertools::Itertools;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
pub type Result<T> = ::std::result::Result<T, failure::Error>;
|
||||
|
||||
const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
|
||||
pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs";
|
||||
pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera";
|
||||
pub const AST: &str = "ra_syntax/src/ast/generated.rs";
|
||||
pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera";
|
||||
pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
|
||||
pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera";
|
||||
pub const AST: &str = "ra_syntax/src/ast/generated.rs.tera";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Test {
|
||||
|
@ -76,43 +69,6 @@ pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn render_template(template: &Path) -> Result<String> {
|
||||
let grammar: ron::value::Value = {
|
||||
let text = fs::read_to_string(project_root().join(GRAMMAR))?;
|
||||
ron::de::from_str(&text)?
|
||||
};
|
||||
let template = fs::read_to_string(template)?;
|
||||
let mut tera = tera::Tera::default();
|
||||
tera.add_raw_template("grammar", &template)
|
||||
.map_err(|e| format_err!("template error: {:?}", e))?;
|
||||
tera.register_function("concat", Box::new(concat));
|
||||
tera.register_filter("camel", |arg, _| {
|
||||
Ok(arg.as_str().unwrap().to_camel_case().into())
|
||||
});
|
||||
tera.register_filter("snake", |arg, _| {
|
||||
Ok(arg.as_str().unwrap().to_snake_case().into())
|
||||
});
|
||||
tera.register_filter("SCREAM", |arg, _| {
|
||||
Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
|
||||
});
|
||||
let ret = tera
|
||||
.render("grammar", &grammar)
|
||||
.map_err(|e| format_err!("template error: {:?}", e))?;
|
||||
return Ok(ret);
|
||||
|
||||
fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
|
||||
let mut elements = Vec::new();
|
||||
for &key in ["a", "b", "c"].iter() {
|
||||
let val = match args.get(key) {
|
||||
Some(val) => val,
|
||||
None => continue,
|
||||
};
|
||||
let val = val.as_array().unwrap();
|
||||
elements.extend(val.iter().cloned());
|
||||
}
|
||||
Ok(tera::Value::Array(elements))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn project_root() -> PathBuf {
|
||||
Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue