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
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 24 additions and 2 deletions

View File

@ -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<String> = 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())
}

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