From 743106684ae832d8e804413d7d1d6cb04ade0fd3 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 10 Apr 2020 20:27:27 -0400 Subject: [PATCH] compiling templates in the same manner as the shim --- src/bin.rs | 31 ++++++++++++++++++++++++++++++- src/lib.rs | 1 + src/renderer/mod.rs | 6 ++++++ src/renderer/renderer.rs | 19 +++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/renderer/mod.rs create mode 100644 src/renderer/renderer.rs diff --git a/src/bin.rs b/src/bin.rs index 43f7650..657e41b 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -1,13 +1,42 @@ extern crate nom; -use parser::template; +use renderer::compile_template; +use renderer::CompiledTemplate; +use std::env; +use std::fs; use std::io::{self, Read}; +use std::path::Path; mod parser; +mod renderer; fn main() { let context = read_context_from_stdin(); println!("{:?}", context); + + let argv: Vec = 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 = 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()) } fn read_context_from_stdin() -> serde_json::map::Map { diff --git a/src/lib.rs b/src/lib.rs index a2ebd73..1f08a84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ extern crate nom; mod parser; +mod renderer; diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs new file mode 100644 index 0000000..8ac447b --- /dev/null +++ b/src/renderer/mod.rs @@ -0,0 +1,6 @@ +//! This module contains a renderer for a rust implementation of LinkedIn Dust + +mod renderer; + +pub use renderer::compile_template; +pub use renderer::CompiledTemplate; diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs new file mode 100644 index 0000000..60ed09a --- /dev/null +++ b/src/renderer/renderer.rs @@ -0,0 +1,19 @@ +use crate::parser::template; +use crate::parser::Template; +use nom::IResult; + +#[derive(Clone, Debug)] +pub struct CompiledTemplate<'a> { + template: Template<'a>, + name: String, +} + +pub fn compile_template<'a>(source: &'a str, name: String) -> CompiledTemplate<'a> { + // TODO: Make this function return a result with a custom error type. Break the nom IResult chain + // TODO: Make this all consuming + let (_remaining, parsed_template) = template(source).expect("Failed to compile template"); + CompiledTemplate { + template: parsed_template, + name: name, + } +}