Loading the compiled templates into a context

This commit is contained in:
Tom Alexander
2020-04-10 20:55:44 -04:00
parent 743106684a
commit 2842d0a14a
3 changed files with 24 additions and 2 deletions

View File

@@ -4,3 +4,4 @@ mod renderer;
pub use renderer::compile_template;
pub use renderer::CompiledTemplate;
pub use renderer::DustContext;

View File

@@ -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);
}
}