Permalink
Browse files

Refactored some of build.rs

  • Loading branch information...
Aaronepower committed May 12, 2018
1 parent 9d3103e commit 51c04a030d444755ea69c1e30572898cd8bd6a0a
Showing with 25 additions and 31 deletions.
  1. +25 −31 build.rs
@@ -2,31 +2,31 @@ extern crate handlebars;
extern crate ignore;
extern crate serde_json;
use std::env;
use std::fs::File;
use std::path::Path;
use std::{cmp, env, error};
use std::ffi::OsStr;
use std::fs::{self, File};
use std::path::Path;
use handlebars::Handlebars;
use serde_json::Value;
use ignore::Walk;
fn main() {
let out_dir = env::var_os("OUT_DIR").expect("can't get OUT_DIR");
generate_languages(&out_dir);
generate_tests(&out_dir);
fn main() -> Result<(), Box<error::Error>> {
let out_dir = env::var_os("OUT_DIR").expect("No OUT_DIR variable.");
generate_languages(&out_dir)?;
generate_tests(&out_dir)?;
Ok(())
}
fn generate_languages(out_dir: &OsStr) {
fn generate_languages(out_dir: &OsStr) -> Result<(), Box<error::Error>> {
let handlebars = {
let mut h = Handlebars::new();
h.register_escape_fn(handlebars::no_escape);
h
};
let mut json: Value = {
let json = File::open(&"languages.json").expect("Cant open json");
serde_json::from_reader(json).expect("Can't parse json")
};
let mut json: Value = serde_json::from_reader(File::open(&"languages.json")?)?;
for (_key, ref mut item) in json.get_mut("languages")
.unwrap()
@@ -49,19 +49,16 @@ fn generate_languages(out_dir: &OsStr) {
}
let output = Path::new(&out_dir).join("language_type.rs");
let mut source_template = File::open(&"src/language/language_type.hbs.rs")
.expect("Can't find Template");
let mut output_file = File::create(&output).expect("Can't create output");
let mut source_template = File::open(&"src/language/language_type.hbs.rs")?;
let mut output_file = File::create(&output)?;
if let Err(err) = handlebars.render_template_source_to_write(&mut source_template,
&json,
&mut output_file)
{
panic!("Failed to generate languages! ERROR: {:?}", err);
}
handlebars.render_template_source_to_write(&mut source_template,
&json,
&mut output_file)?;
Ok(())
}
fn compare_json_str_len(a: &Value, b: &Value) -> ::std::cmp::Ordering {
fn compare_json_str_len(a: &Value, b: &Value) -> cmp::Ordering {
let a = a.as_array().expect("a as array");
let b = b.as_array().expect("b as array");
@@ -71,12 +68,10 @@ fn compare_json_str_len(a: &Value, b: &Value) -> ::std::cmp::Ordering {
max_b_size.cmp(&max_a_size)
}
fn generate_tests(out_dir: &OsStr) {
use std::io::Write;
use ignore::Walk;
let mut string = String::new();
fn generate_tests(out_dir: &OsStr) -> Result<(), Box<error::Error>> {
// Length of string literal below by number of languages
const INITIAL_BUFFER_SIZE: usize = 989 * 130;
let mut string = String::with_capacity(INITIAL_BUFFER_SIZE);
let walker = Walk::new("./tests/data/").filter(|p| {
match p {
@@ -92,7 +87,7 @@ fn generate_tests(out_dir: &OsStr) {
});
for path in walker {
let path = path.unwrap();
let path = path?;
let path = path.path();
let name = path.file_stem().unwrap().to_str().unwrap().to_lowercase();
@@ -124,6 +119,5 @@ fn generate_tests(out_dir: &OsStr) {
"#, name, path.display()));
}
File::create(Path::new(&out_dir).join("tests.rs")).unwrap()
.write_all(string.as_bytes()).unwrap();
Ok(fs::write(Path::new(&out_dir).join("tests.rs"), string)?)
}

0 comments on commit 51c04a0

Please sign in to comment.