2020-04-05 23:12:48 +00:00
|
|
|
extern crate nom;
|
2020-04-10 23:07:02 +00:00
|
|
|
|
2020-05-31 23:01:51 +00:00
|
|
|
use parser::Template;
|
2020-04-11 00:27:27 +00:00
|
|
|
use renderer::compile_template;
|
2020-05-31 23:01:51 +00:00
|
|
|
use renderer::CompileError;
|
2020-04-11 00:58:55 +00:00
|
|
|
use renderer::DustRenderer;
|
2020-04-11 00:27:27 +00:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2020-04-10 23:07:02 +00:00
|
|
|
use std::io::{self, Read};
|
2020-04-11 00:27:27 +00:00
|
|
|
use std::path::Path;
|
2020-04-05 23:12:48 +00:00
|
|
|
|
2020-12-29 23:21:12 +00:00
|
|
|
mod integrations;
|
2020-04-05 23:12:48 +00:00
|
|
|
mod parser;
|
2020-04-11 00:27:27 +00:00
|
|
|
mod renderer;
|
2020-04-05 23:12:48 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-04-10 23:07:02 +00:00
|
|
|
let context = read_context_from_stdin();
|
2020-04-11 00:27:27 +00: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();
|
2020-05-31 23:08:10 +00:00
|
|
|
let compiled_templates_result: Result<Vec<(String, Template)>, CompileError> =
|
|
|
|
template_contents
|
|
|
|
.iter()
|
|
|
|
.map(|(p, contents)| template_from_file(p, contents))
|
|
|
|
.collect();
|
|
|
|
let compiled_templates = compiled_templates_result.unwrap();
|
|
|
|
let main_template_name = &compiled_templates
|
|
|
|
.first()
|
|
|
|
.expect("There should be more than 1 template")
|
|
|
|
.0;
|
|
|
|
let mut dust_renderer = DustRenderer::new();
|
|
|
|
compiled_templates.iter().for_each(|(name, template)| {
|
|
|
|
dust_renderer.load_source(template, name.to_owned());
|
|
|
|
});
|
2020-06-07 04:17:55 +00:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
dust_renderer
|
|
|
|
.render(main_template_name, Some(&context))
|
|
|
|
.expect("Failed to render")
|
|
|
|
);
|
2020-04-11 00:27:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 23:01:51 +00:00
|
|
|
fn template_from_file<'a>(
|
|
|
|
file_path: &str,
|
|
|
|
file_contents: &'a str,
|
|
|
|
) -> Result<(String, Template<'a>), CompileError> {
|
2020-04-11 00:27:27 +00:00
|
|
|
let path: &Path = Path::new(file_path);
|
2020-05-31 23:01:51 +00:00
|
|
|
let name = path.file_stem().ok_or(CompileError {
|
|
|
|
message: format!("Failed to get file stem on {}", file_path),
|
|
|
|
})?;
|
|
|
|
Ok((
|
|
|
|
name.to_string_lossy().to_string(),
|
|
|
|
compile_template(file_contents)?,
|
|
|
|
))
|
2020-04-10 23:07:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 02:52:20 +00:00
|
|
|
fn read_context_from_stdin() -> serde_json::Value {
|
2020-04-10 23:07:02 +00:00
|
|
|
let mut buffer = String::new();
|
|
|
|
io::stdin()
|
|
|
|
.read_to_string(&mut buffer)
|
|
|
|
.expect("Failed to read stdin");
|
|
|
|
|
2020-05-03 19:29:02 +00:00
|
|
|
serde_json::from_str(&buffer).expect("Failed to parse json")
|
2020-04-05 23:12:48 +00:00
|
|
|
}
|