From 2842d0a14aeeb5e9b9586286b75f60ab653acc90 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 10 Apr 2020 20:55:44 -0400 Subject: [PATCH] Loading the compiled templates into a context --- src/bin.rs | 7 +++++-- src/renderer/mod.rs | 1 + src/renderer/renderer.rs | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index 657e41b..607e8d8 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -2,6 +2,7 @@ extern crate nom; use renderer::compile_template; use renderer::CompiledTemplate; +use renderer::DustContext; use std::env; use std::fs; use std::io::{self, Read}; @@ -12,7 +13,6 @@ mod renderer; fn main() { let context = read_context_from_stdin(); - println!("{:?}", context); let argv: Vec = env::args().collect(); if argv.len() < 2 { @@ -30,12 +30,15 @@ fn main() { .iter() .map(|(p, contents)| template_from_file(p, contents)) .collect(); + let mut dust_context = DustContext::new(); + compiled_templates.iter().for_each(|template| { + dust_context.load_source(template); + }); } 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()) } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 8ac447b..a259b5d 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -4,3 +4,4 @@ mod renderer; pub use renderer::compile_template; pub use renderer::CompiledTemplate; +pub use renderer::DustContext; diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 60ed09a..7873827 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -1,6 +1,7 @@ use crate::parser::template; use crate::parser::Template; use nom::IResult; +use std::collections::BTreeMap; #[derive(Clone, Debug)] pub struct CompiledTemplate<'a> { @@ -8,6 +9,11 @@ pub struct CompiledTemplate<'a> { name: String, } +#[derive(Clone, Debug)] +pub struct DustContext<'a> { + templates: BTreeMap<&'a String, &'a Template<'a>>, +} + 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 @@ -17,3 +23,15 @@ pub fn compile_template<'a>(source: &'a str, name: String) -> CompiledTemplate<' name: name, } } + +impl<'a> DustContext<'a> { + pub fn new() -> DustContext<'a> { + DustContext { + templates: BTreeMap::new(), + } + } + + pub fn load_source(&mut self, template: &'a CompiledTemplate) { + self.templates.insert(&template.name, &template.template); + } +}