2020-04-05 23:12:48 +00:00
|
|
|
extern crate nom;
|
2020-04-10 23:07:02 +00:00
|
|
|
|
2020-04-11 00:27:27 +00:00
|
|
|
use renderer::compile_template;
|
|
|
|
use renderer::CompiledTemplate;
|
2020-04-12 02:47:31 +00:00
|
|
|
use renderer::ContextElement;
|
2020-04-11 00:58:55 +00:00
|
|
|
use renderer::DustRenderer;
|
2020-04-11 23:11:14 +00:00
|
|
|
use renderer::Renderable;
|
2020-04-12 02:47:31 +00:00
|
|
|
use renderer::Walkable;
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
let compiled_templates: Vec<CompiledTemplate> = template_contents
|
|
|
|
.iter()
|
|
|
|
.map(|(p, contents)| template_from_file(p, contents))
|
|
|
|
.collect();
|
2020-04-11 22:25:48 +00:00
|
|
|
let mut dust_renderer = DustRenderer::new();
|
2020-04-11 00:55:44 +00:00
|
|
|
compiled_templates.iter().for_each(|template| {
|
2020-04-11 22:25:48 +00:00
|
|
|
dust_renderer.load_source(template);
|
2020-04-11 00:55:44 +00:00
|
|
|
});
|
2020-04-11 22:25:48 +00:00
|
|
|
let main_template_name = &compiled_templates
|
|
|
|
.first()
|
|
|
|
.expect("There should be more than 1 template")
|
|
|
|
.name;
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
dust_renderer
|
2020-04-11 22:40:36 +00:00
|
|
|
.render(main_template_name, &context)
|
2020-04-11 22:25:48 +00:00
|
|
|
.expect("Failed to render")
|
|
|
|
);
|
2020-04-11 00:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
compile_template(file_contents, name.to_string_lossy().to_string())
|
2020-04-11 22:25:48 +00:00
|
|
|
.expect("Failed to compile template")
|
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");
|
|
|
|
|
|
|
|
let parsed: serde_json::Value = serde_json::from_str(&buffer).expect("Failed to parse json");
|
|
|
|
match parsed {
|
2020-04-12 02:52:20 +00:00
|
|
|
serde_json::Value::Object(obj) => serde_json::value::Value::Object(obj),
|
2020-04-10 23:07:02 +00:00
|
|
|
_ => panic!("Expected context to be an object"),
|
|
|
|
}
|
2020-04-05 23:12:48 +00:00
|
|
|
}
|
2020-04-11 23:11:14 +00:00
|
|
|
|
2020-04-12 02:47:31 +00:00
|
|
|
impl ContextElement for serde_json::Value {}
|
|
|
|
|
2020-04-11 23:11:14 +00:00
|
|
|
impl Renderable for serde_json::Value {
|
|
|
|
fn render(&self) -> String {
|
2020-04-11 23:19:40 +00:00
|
|
|
match self {
|
|
|
|
serde_json::Value::Null => "".to_owned(),
|
|
|
|
serde_json::Value::Bool(boolean) => boolean.to_string(),
|
|
|
|
serde_json::Value::Number(num) => num.to_string(),
|
|
|
|
serde_json::Value::String(string) => string.to_string(),
|
|
|
|
serde_json::Value::Array(_arr) => panic!("Attempted to render an array"),
|
|
|
|
serde_json::Value::Object(_obj) => panic!("Attempted to render an object"),
|
|
|
|
}
|
2020-04-11 23:11:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 02:47:31 +00:00
|
|
|
|
|
|
|
impl Walkable for serde_json::Value {
|
|
|
|
fn walk(&self, segment: &str) -> &dyn ContextElement {
|
|
|
|
match self {
|
|
|
|
serde_json::Value::Null => panic!("Attempted to walk to {} on a null value", segment),
|
|
|
|
serde_json::Value::Bool(_boolean) => {
|
|
|
|
panic!("Attempted to walk to {} on a boolean value", segment)
|
|
|
|
}
|
|
|
|
serde_json::Value::Number(_num) => {
|
|
|
|
panic!("Attempted to walk to {} on a number value", segment)
|
|
|
|
}
|
|
|
|
serde_json::Value::String(_string) => {
|
|
|
|
panic!("Attempted to walk to {} on a string value", segment)
|
|
|
|
}
|
|
|
|
serde_json::Value::Array(_arr) => todo!("Arrays not supported yet"),
|
|
|
|
serde_json::Value::Object(obj) => obj.get(segment).unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|