Skip to content

Commit

Permalink
Move codegen to use Tera
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed May 28, 2020
1 parent 0b98f31 commit 2342baa
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 380 deletions.
179 changes: 164 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ lto = "thin"
panic = "abort"

[build-dependencies]
handlebars = "3.0.1"
tera = "1.3.0"
ignore = "0.4.15"
serde_json = "1.0.53"

Expand Down
15 changes: 4 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate handlebars;
extern crate ignore;
extern crate serde_json;

Expand All @@ -7,7 +6,6 @@ use std::fs::{self, File};
use std::path::Path;
use std::{cmp, env, error};

use handlebars::Handlebars;
use ignore::Walk;
use serde_json::Value;

Expand All @@ -20,11 +18,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
}

fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
let handlebars = {
let mut h = Handlebars::new();
h.register_escape_fn(handlebars::no_escape);
h
};
let mut tera = tera::Tera::default();

let mut json: Value = serde_json::from_reader(File::open(&"languages.json")?)?;

Expand All @@ -50,11 +44,10 @@ fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
sort_prop!("multi_line");
}

let output = Path::new(&out_dir).join("language_type.rs");
let mut source_template = File::open(&"src/language/language_type.hbs.rs")?;
let mut output_file = File::create(&output)?;
let output_path = Path::new(&out_dir).join("language_type.rs");
let rust_code = tera.render_str(&std::fs::read_to_string("src/language/language_type.hbs.rs")?, &tera::Context::from_value(json)?)?;
std::fs::write(output_path, rust_code)?;

handlebars.render_template_source_to_write(&mut source_template, &json, &mut output_file)?;
Ok(())
}

Expand Down
Loading

0 comments on commit 2342baa

Please sign in to comment.