diff options
-rw-r--r-- | Cargo.lock | 4 | ||||
-rw-r--r-- | crates/teraron/Cargo.toml | 13 | ||||
-rw-r--r-- | crates/teraron/src/lib.rs | 109 | ||||
-rw-r--r-- | crates/tools/Cargo.toml | 2 |
4 files changed, 4 insertions, 124 deletions
diff --git a/Cargo.lock b/Cargo.lock index a4a07e004..79da84ebc 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1024,6 +1024,7 @@ dependencies = [ | |||
1024 | [[package]] | 1024 | [[package]] |
1025 | name = "teraron" | 1025 | name = "teraron" |
1026 | version = "0.0.1" | 1026 | version = "0.0.1" |
1027 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1027 | dependencies = [ | 1028 | dependencies = [ |
1028 | "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", | 1029 | "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 | "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", |
@@ -1091,7 +1092,7 @@ dependencies = [ | |||
1091 | "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1092 | "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1092 | "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", | 1093 | "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", |
1093 | "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", | 1094 | "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", |
1094 | "teraron 0.0.1", | 1095 | "teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", |
1095 | "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", | 1096 | "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", |
1096 | ] | 1097 | ] |
1097 | 1098 | ||
@@ -1380,6 +1381,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1380 | "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" | 1381 | "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" |
1381 | "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" | 1382 | "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" |
1382 | "checksum tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6c87cae42cc4fc480278c7583792cc5da2d51a25be916b7921cbb45c43063b8d" | 1383 | "checksum tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6c87cae42cc4fc480278c7583792cc5da2d51a25be916b7921cbb45c43063b8d" |
1384 | "checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e" | ||
1383 | "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" | 1385 | "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" |
1384 | "checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9" | 1386 | "checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9" |
1385 | "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" | 1387 | "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 deleted file mode 100644 index 7ce9f1aa8..000000000 --- a/crates/teraron/Cargo.toml +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | [package] | ||
2 | name = "teraron" | ||
3 | version = "0.0.1" | ||
4 | authors = ["Aleksey Kladov <[email protected]>"] | ||
5 | repository = "https://github.com/rust-analyzer/teraron" | ||
6 | description = "Genrate Rust code from a .tera template and a .ron data" | ||
7 | license = "MIT OR Apache-2.0" | ||
8 | |||
9 | [dependencies] | ||
10 | failure = "0.1.2" | ||
11 | tera = "0.11.18" | ||
12 | ron = "0.4.0" | ||
13 | heck = "0.3.0" | ||
diff --git a/crates/teraron/src/lib.rs b/crates/teraron/src/lib.rs deleted file mode 100644 index e464ea080..000000000 --- a/crates/teraron/src/lib.rs +++ /dev/null | |||
@@ -1,109 +0,0 @@ | |||
1 | //! A simple tool to generate rust code by passing a ron value | ||
2 | //! to a tera template | ||
3 | |||
4 | #[macro_use] | ||
5 | extern crate failure; | ||
6 | extern crate tera; | ||
7 | extern crate ron; | ||
8 | extern crate heck; | ||
9 | |||
10 | use std::{ | ||
11 | fs, | ||
12 | collections::HashMap, | ||
13 | path::Path, | ||
14 | }; | ||
15 | |||
16 | use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; | ||
17 | |||
18 | #[derive(Clone, Copy, PartialEq, Eq)] | ||
19 | pub enum Mode { | ||
20 | Overwrite, | ||
21 | Verify, | ||
22 | } | ||
23 | |||
24 | pub use Mode::*; | ||
25 | |||
26 | /// A helper to update file on disk if it has changed. | ||
27 | /// With verify = false, | ||
28 | pub 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 | |||
43 | pub 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 | |||
71 | pub 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 | |||
83 | fn 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 | |||
98 | fn 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 ffd0ab1a5..41b19c5c6 100644 --- a/crates/tools/Cargo.toml +++ b/crates/tools/Cargo.toml | |||
@@ -6,7 +6,7 @@ authors = ["Aleksey Kladov <[email protected]>"] | |||
6 | publish = false | 6 | publish = false |
7 | 7 | ||
8 | [dependencies] | 8 | [dependencies] |
9 | teraron = { path = "../teraron"} | 9 | teraron = "0.0.1" |
10 | walkdir = "2.1.3" | 10 | walkdir = "2.1.3" |
11 | itertools = "0.7.8" | 11 | itertools = "0.7.8" |
12 | clap = "2.32.0" | 12 | clap = "2.32.0" |