Switch to published teraron

This commit is contained in:
Aleksey Kladov 2018-10-16 20:36:45 +03:00
parent 514aa3cf85
commit 0b6d4983de
4 changed files with 4 additions and 124 deletions

4
Cargo.lock generated
View File

@ -1024,6 +1024,7 @@ dependencies = [
[[package]]
name = "teraron"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1091,7 +1092,7 @@ dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
"teraron 0.0.1",
"teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1380,6 +1381,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7"
"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
"checksum tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6c87cae42cc4fc480278c7583792cc5da2d51a25be916b7921cbb45c43063b8d"
"checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9"
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"

View File

@ -1,13 +0,0 @@
[package]
name = "teraron"
version = "0.0.1"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
repository = "https://github.com/rust-analyzer/teraron"
description = "Genrate Rust code from a .tera template and a .ron data"
license = "MIT OR Apache-2.0"
[dependencies]
failure = "0.1.2"
tera = "0.11.18"
ron = "0.4.0"
heck = "0.3.0"

View File

@ -1,109 +0,0 @@
//! A simple tool to generate rust code by passing a ron value
//! to a tera template
#[macro_use]
extern crate failure;
extern crate tera;
extern crate ron;
extern crate heck;
use std::{
fs,
collections::HashMap,
path::Path,
};
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Mode {
Overwrite,
Verify,
}
pub use Mode::*;
/// A helper to update file on disk if it has changed.
/// With verify = false,
pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<(), failure::Error> {
match fs::read_to_string(path) {
Ok(ref old_contents) if old_contents == contents => {
return Ok(());
}
_ => (),
}
if mode == Verify {
bail!("`{}` is not up-to-date", path.display());
}
eprintln!("updating {}", path.display());
fs::write(path, contents)?;
Ok(())
}
pub fn generate(
template: &Path,
src: &Path,
mode: Mode,
) -> Result<(), failure::Error> {
assert_eq!(
template.extension().and_then(|it| it.to_str()), Some("tera"),
"template file must have .rs.tera extension",
);
let file_name = template.file_stem().unwrap().to_str().unwrap();
assert!(
file_name.ends_with(".rs"),
"template file must have .rs.tera extension",
);
let tgt = template.with_file_name(file_name);
let template = fs::read_to_string(template)?;
let src: ron::Value = {
let text = fs::read_to_string(src)?;
ron::de::from_str(&text)?
};
let content = render(&template, src)?;
update(
&tgt,
&content,
mode,
)
}
pub fn render(
template: &str,
src: ron::Value,
) -> Result<String, failure::Error> {
let mut tera = mk_tera();
tera.add_raw_template("_src", template)
.map_err(|e| format_err!("template parsing error: {:?}", e))?;
let res = tera.render("_src", &src)
.map_err(|e| format_err!("template rendering error: {:?}", e))?;
return Ok(res);
}
fn mk_tera() -> tera::Tera {
let mut res = tera::Tera::default();
res.register_filter("camel", |arg, _| {
Ok(arg.as_str().unwrap().to_camel_case().into())
});
res.register_filter("snake", |arg, _| {
Ok(arg.as_str().unwrap().to_snake_case().into())
});
res.register_filter("SCREAM", |arg, _| {
Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
});
res.register_function("concat", Box::new(concat));
res
}
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))
}

View File

@ -6,7 +6,7 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
publish = false
[dependencies]
teraron = { path = "../teraron"}
teraron = "0.0.1"
walkdir = "2.1.3"
itertools = "0.7.8"
clap = "2.32.0"