aboutsummaryrefslogtreecommitdiff
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
parent1216878f7be20dd0e652fb8cdc395009fdcfae07 (diff)
extract teraron
-rw-r--r--Cargo.lock22
-rw-r--r--crates/teraron/Cargo.toml13
-rw-r--r--crates/teraron/src/lib.rs109
-rw-r--r--crates/tools/Cargo.toml4
-rw-r--r--crates/tools/src/lib.rs50
-rw-r--r--crates/tools/src/main.rs45
6 files changed, 164 insertions, 79 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 707257a31..a4a07e004 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -794,7 +794,7 @@ dependencies = [
794 794
795[[package]] 795[[package]]
796name = "ron" 796name = "ron"
797version = "0.3.0" 797version = "0.4.0"
798source = "registry+https://github.com/rust-lang/crates.io-index" 798source = "registry+https://github.com/rust-lang/crates.io-index"
799dependencies = [ 799dependencies = [
800 "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", 800 "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1003,7 +1003,7 @@ dependencies = [
1003 1003
1004[[package]] 1004[[package]]
1005name = "tera" 1005name = "tera"
1006version = "0.11.17" 1006version = "0.11.18"
1007source = "registry+https://github.com/rust-lang/crates.io-index" 1007source = "registry+https://github.com/rust-lang/crates.io-index"
1008dependencies = [ 1008dependencies = [
1009 "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", 1009 "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1022,6 +1022,16 @@ dependencies = [
1022] 1022]
1023 1023
1024[[package]] 1024[[package]]
1025name = "teraron"
1026version = "0.0.1"
1027dependencies = [
1028 "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
1029 "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
1030 "ron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
1031 "tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)",
1032]
1033
1034[[package]]
1025name = "termion" 1035name = "termion"
1026version = "1.5.1" 1036version = "1.5.1"
1027source = "registry+https://github.com/rust-lang/crates.io-index" 1037source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1080,10 +1090,8 @@ version = "0.1.0"
1080dependencies = [ 1090dependencies = [
1081 "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", 1091 "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
1082 "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 1092 "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
1083 "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
1084 "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", 1093 "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
1085 "ron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 1094 "teraron 0.0.1",
1086 "tera 0.11.17 (registry+https://github.com/rust-lang/crates.io-index)",
1087 "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 1095 "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
1088] 1096]
1089 1097
@@ -1345,7 +1353,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1345"checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" 1353"checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d"
1346"checksum relative-path 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "e614f96449605730b4f7ad2c019e88c1652d730634b4eba07b810801856635e3" 1354"checksum relative-path 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "e614f96449605730b4f7ad2c019e88c1652d730634b4eba07b810801856635e3"
1347"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" 1355"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5"
1348"checksum ron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9fa11b7a38511d46ff1959ae46ebb60bd8a746f17bdd0206b4c8de7559ac47b" 1356"checksum ron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c48677d8a9247a4e0d1f3f9cb4b0a8e29167fdc3c04f383a5e669cd7a960ae0f"
1349"checksum rowan 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4a1a7366ece9deee5e7df8316a136d585d5c5042854c2297f7f1aee3014c9130" 1357"checksum rowan 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4a1a7366ece9deee5e7df8316a136d585d5c5042854c2297f7f1aee3014c9130"
1350"checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" 1358"checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395"
1351"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" 1359"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8"
@@ -1371,7 +1379,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1371"checksum syn 0.15.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b036b7b35e846707c0e55c2c9441fa47867c0f87fca416921db3261b1d8c741a" 1379"checksum syn 0.15.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b036b7b35e846707c0e55c2c9441fa47867c0f87fca416921db3261b1d8c741a"
1372"checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" 1380"checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7"
1373"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" 1381"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
1374"checksum tera 0.11.17 (registry+https://github.com/rust-lang/crates.io-index)" = "2829d259c4699fbbe8acb353d231e6da31ff4301c52244413ed29ff6093da412" 1382"checksum tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6c87cae42cc4fc480278c7583792cc5da2d51a25be916b7921cbb45c43063b8d"
1375"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" 1383"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
1376"checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9" 1384"checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9"
1377"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" 1385"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
diff --git a/crates/teraron/Cargo.toml b/crates/teraron/Cargo.toml
new file mode 100644
index 000000000..7ce9f1aa8
--- /dev/null
+++ b/crates/teraron/Cargo.toml
@@ -0,0 +1,13 @@
1[package]
2name = "teraron"
3version = "0.0.1"
4authors = ["Aleksey Kladov <[email protected]>"]
5repository = "https://github.com/rust-analyzer/teraron"
6description = "Genrate Rust code from a .tera template and a .ron data"
7license = "MIT OR Apache-2.0"
8
9[dependencies]
10failure = "0.1.2"
11tera = "0.11.18"
12ron = "0.4.0"
13heck = "0.3.0"
diff --git a/crates/teraron/src/lib.rs b/crates/teraron/src/lib.rs
new file mode 100644
index 000000000..e464ea080
--- /dev/null
+++ b/crates/teraron/src/lib.rs
@@ -0,0 +1,109 @@
1//! A simple tool to generate rust code by passing a ron value
2//! to a tera template
3
4#[macro_use]
5extern crate failure;
6extern crate tera;
7extern crate ron;
8extern crate heck;
9
10use std::{
11 fs,
12 collections::HashMap,
13 path::Path,
14};
15
16use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
17
18#[derive(Clone, Copy, PartialEq, Eq)]
19pub enum Mode {
20 Overwrite,
21 Verify,
22}
23
24pub use Mode::*;
25
26/// A helper to update file on disk if it has changed.
27/// With verify = false,
28pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<(), failure::Error> {
29 match fs::read_to_string(path) {
30 Ok(ref old_contents) if old_contents == contents => {
31 return Ok(());
32 }
33 _ => (),
34 }
35 if mode == Verify {
36 bail!("`{}` is not up-to-date", path.display());
37 }
38 eprintln!("updating {}", path.display());
39 fs::write(path, contents)?;
40 Ok(())
41}
42
43pub fn generate(
44 template: &Path,
45 src: &Path,
46 mode: Mode,
47) -> Result<(), failure::Error> {
48 assert_eq!(
49 template.extension().and_then(|it| it.to_str()), Some("tera"),
50 "template file must have .rs.tera extension",
51 );
52 let file_name = template.file_stem().unwrap().to_str().unwrap();
53 assert!(
54 file_name.ends_with(".rs"),
55 "template file must have .rs.tera extension",
56 );
57 let tgt = template.with_file_name(file_name);
58 let template = fs::read_to_string(template)?;
59 let src: ron::Value = {
60 let text = fs::read_to_string(src)?;
61 ron::de::from_str(&text)?
62 };
63 let content = render(&template, src)?;
64 update(
65 &tgt,
66 &content,
67 mode,
68 )
69}
70
71pub fn render(
72 template: &str,
73 src: ron::Value,
74) -> Result<String, failure::Error> {
75 let mut tera = mk_tera();
76 tera.add_raw_template("_src", template)
77 .map_err(|e| format_err!("template parsing error: {:?}", e))?;
78 let res = tera.render("_src", &src)
79 .map_err(|e| format_err!("template rendering error: {:?}", e))?;
80 return Ok(res);
81}
82
83fn mk_tera() -> tera::Tera {
84 let mut res = tera::Tera::default();
85 res.register_filter("camel", |arg, _| {
86 Ok(arg.as_str().unwrap().to_camel_case().into())
87 });
88 res.register_filter("snake", |arg, _| {
89 Ok(arg.as_str().unwrap().to_snake_case().into())
90 });
91 res.register_filter("SCREAM", |arg, _| {
92 Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
93 });
94 res.register_function("concat", Box::new(concat));
95 res
96}
97
98fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
99 let mut elements = Vec::new();
100 for &key in ["a", "b", "c"].iter() {
101 let val = match args.get(key) {
102 Some(val) => val,
103 None => continue,
104 };
105 let val = val.as_array().unwrap();
106 elements.extend(val.iter().cloned());
107 }
108 Ok(tera::Value::Array(elements))
109}
diff --git a/crates/tools/Cargo.toml b/crates/tools/Cargo.toml
index e2fecc60d..ffd0ab1a5 100644
--- a/crates/tools/Cargo.toml
+++ b/crates/tools/Cargo.toml
@@ -6,10 +6,8 @@ authors = ["Aleksey Kladov <[email protected]>"]
6publish = false 6publish = false
7 7
8[dependencies] 8[dependencies]
9ron = "0.3.0" 9teraron = { path = "../teraron"}
10walkdir = "2.1.3" 10walkdir = "2.1.3"
11itertools = "0.7.8" 11itertools = "0.7.8"
12tera = "0.11"
13clap = "2.32.0" 12clap = "2.32.0"
14failure = "0.1.1" 13failure = "0.1.1"
15heck = "0.3.0"
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())
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index b662d78df..1bbc43123 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -3,6 +3,7 @@ extern crate clap;
3extern crate failure; 3extern crate failure;
4extern crate tools; 4extern crate tools;
5extern crate walkdir; 5extern crate walkdir;
6extern crate teraron;
6 7
7use clap::{App, Arg, SubCommand}; 8use clap::{App, Arg, SubCommand};
8use std::{ 9use std::{
@@ -12,9 +13,9 @@ use std::{
12 process::Command, 13 process::Command,
13}; 14};
14use tools::{ 15use tools::{
15 collect_tests, project_root, render_template, update, Result, Test, AST, AST_TEMPLATE, 16 collect_tests, project_root, Result, Test, AST, SYNTAX_KINDS, GRAMMAR,
16 SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE,
17}; 17};
18use teraron::{Mode, Verify, Overwrite};
18 19
19const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; 20const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
20const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; 21const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
@@ -32,35 +33,35 @@ fn main() -> Result<()> {
32 .subcommand(SubCommand::with_name("gen-tests")) 33 .subcommand(SubCommand::with_name("gen-tests"))
33 .subcommand(SubCommand::with_name("install-code")) 34 .subcommand(SubCommand::with_name("install-code"))
34 .get_matches(); 35 .get_matches();
36 let mode = if matches.is_present("verify") {
37 Verify
38 } else {
39 Overwrite
40 };
35 match matches.subcommand() { 41 match matches.subcommand() {
36 ("install-code", _) => install_code_extension()?, 42 ("install-code", _) => install_code_extension()?,
37 (name, Some(matches)) => run_gen_command(name, matches.is_present("verify"))?, 43 ("gen-tests", _) => gen_tests(mode)?,
38 _ => unreachable!(), 44 ("gen-kinds", _) => {
39 } 45 let grammar = project_root().join(GRAMMAR);
40 Ok(()) 46 let syntax_kinds = project_root().join(SYNTAX_KINDS);
41} 47 let ast = project_root().join(AST);
42 48 teraron::generate(
43fn run_gen_command(name: &str, verify: bool) -> Result<()> { 49 &syntax_kinds,
44 match name { 50 &grammar,
45 "gen-kinds" => { 51 mode,
46 update(
47 &project_root().join(SYNTAX_KINDS),
48 &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE))?,
49 verify,
50 )?; 52 )?;
51 update( 53 teraron::generate(
52 &project_root().join(AST), 54 &ast,
53 &render_template(&project_root().join(AST_TEMPLATE))?, 55 &grammar,
54 verify, 56 mode,
55 )?; 57 )?;
56 } 58 }
57 "gen-tests" => gen_tests(verify)?,
58 _ => unreachable!(), 59 _ => unreachable!(),
59 } 60 }
60 Ok(()) 61 Ok(())
61} 62}
62 63
63fn gen_tests(verify: bool) -> Result<()> { 64fn gen_tests(mode: Mode) -> Result<()> {
64 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; 65 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?;
65 66
66 let inline_tests_dir = Path::new(INLINE_TESTS_DIR); 67 let inline_tests_dir = Path::new(INLINE_TESTS_DIR);
@@ -83,7 +84,7 @@ fn gen_tests(verify: bool) -> Result<()> {
83 inline_tests_dir.join(file_name) 84 inline_tests_dir.join(file_name)
84 } 85 }
85 }; 86 };
86 update(&path, &test.text, verify)?; 87 teraron::update(&path, &test.text, mode)?;
87 } 88 }
88 Ok(()) 89 Ok(())
89} 90}