2020-04-05 19:12:48 -04:00
|
|
|
extern crate nom;
|
2020-04-10 19:07:02 -04:00
|
|
|
|
2020-04-10 20:27:27 -04:00
|
|
|
use renderer::compile_template;
|
|
|
|
use renderer::CompiledTemplate;
|
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2020-04-10 19:07:02 -04:00
|
|
|
use std::io::{self, Read};
|
2020-04-10 20:27:27 -04:00
|
|
|
use std::path::Path;
|
2020-04-05 19:12:48 -04:00
|
|
|
|
|
|
|
mod parser;
|
2020-04-10 20:27:27 -04:00
|
|
|
mod renderer;
|
2020-04-05 19:12:48 -04:00
|
|
|
|
|
|
|
fn main() {
|
2020-04-10 19:07:02 -04:00
|
|
|
let context = read_context_from_stdin();
|
|
|
|
println!("{:?}", context);
|
2020-04-10 20:27:27 -04:00
|
|
|
|
|
|
|
let argv: Vec<String> = env::args().collect();
|
|
|
|
if argv.len() < 2 {
|
|
|
|
panic!("Need to pass templates");
|
|
|
|
}
|
|
|
|
let template_paths = &argv[1..];
|
|
|
|
let template_contents: Vec<(String, String)> = template_paths
|
|
|
|
.iter()
|
|
|
|
.map(|p| {
|
|
|
|
let template_content = fs::read_to_string(&p).unwrap();
|
|
|
|
(p.to_string(), template_content)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let compiled_templates: Vec<CompiledTemplate> = template_contents
|
|
|
|
.iter()
|
|
|
|
.map(|(p, contents)| template_from_file(p, contents))
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn template_from_file<'a>(file_path: &str, file_contents: &'a str) -> CompiledTemplate<'a> {
|
|
|
|
let path: &Path = Path::new(file_path);
|
|
|
|
let name = path.file_stem().unwrap();
|
|
|
|
println!("{:?}", name);
|
|
|
|
compile_template(file_contents, name.to_string_lossy().to_string())
|
2020-04-10 19:07:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_context_from_stdin() -> serde_json::map::Map<String, serde_json::Value> {
|
|
|
|
let mut buffer = String::new();
|
|
|
|
io::stdin()
|
|
|
|
.read_to_string(&mut buffer)
|
|
|
|
.expect("Failed to read stdin");
|
|
|
|
|
|
|
|
let parsed: serde_json::Value = serde_json::from_str(&buffer).expect("Failed to parse json");
|
|
|
|
match parsed {
|
|
|
|
serde_json::Value::Object(obj) => obj,
|
|
|
|
_ => panic!("Expected context to be an object"),
|
|
|
|
}
|
2020-04-05 19:12:48 -04:00
|
|
|
}
|