From 043cc5eda49bfbbc2b4da40d7ac6a95155088e31 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 22 Oct 2023 17:43:30 -0400 Subject: [PATCH] I think I have worked around the lifetime issue by keeping references to the intermediate str's. --- src/command/build/render.rs | 28 +++++++++++++++++----------- src/render/duster_renderer.rs | 23 +++++++++++++---------- src/render/renderer_integration.rs | 7 ++----- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/command/build/render.rs b/src/command/build/render.rs index cb2224d..cac8670 100644 --- a/src/command/build/render.rs +++ b/src/command/build/render.rs @@ -38,12 +38,20 @@ impl SiteRenderer { return Err("Expect exactly 1 main.dust template file.".into()); } - for entry in main_template { - load_template_from_include_dir(&mut renderer_integration, entry)?; - } + let decoded_templates = { + let mut decoded_templates = + Vec::with_capacity(main_template.len() + other_templates.len()); + for entry in main_template { + decoded_templates.push(build_name_contents_pairs(entry)?); + } + for entry in other_templates { + decoded_templates.push(build_name_contents_pairs(entry)?); + } + decoded_templates + }; - for entry in other_templates { - load_template_from_include_dir(&mut renderer_integration, entry)?; + for (name, contents) in decoded_templates { + renderer_integration.load_template(name, contents)?; } for blog_post in &self.blog_posts { @@ -55,10 +63,9 @@ impl SiteRenderer { } } -fn load_template_from_include_dir( - renderer_integration: &mut RI, - entry: &include_dir::File<'_>, -) -> Result<(), CustomError> { +fn build_name_contents_pairs<'a>( + entry: &'a include_dir::File<'_>, +) -> Result<(&'a str, &'a str), CustomError> { let path = entry.path(); let name = path .file_stem() @@ -66,6 +73,5 @@ fn load_template_from_include_dir( .to_str() .ok_or("All template filenames should be valid utf-8.")?; let contents = std::str::from_utf8(entry.contents())?; - renderer_integration.load_template(name, contents)?; - Ok(()) + Ok((name, contents)) } diff --git a/src/render/duster_renderer.rs b/src/render/duster_renderer.rs index 492d84a..4c8a944 100644 --- a/src/render/duster_renderer.rs +++ b/src/render/duster_renderer.rs @@ -1,24 +1,27 @@ +use std::collections::HashMap; + use crate::error::CustomError; use super::renderer_integration::RendererIntegration; use duster::renderer::DustRenderer; use serde::Serialize; -pub(crate) struct DusterRenderer {} +pub(crate) struct DusterRenderer<'a> { + templates: HashMap<&'a str, duster::parser::Template<'a>>, +} -impl DusterRenderer { - pub(crate) fn new() -> DusterRenderer { - DusterRenderer {} +impl<'a> DusterRenderer<'a> { + pub(crate) fn new() -> DusterRenderer<'a> { + DusterRenderer { + templates: HashMap::new(), + } } } -impl RendererIntegration for DusterRenderer { - fn load_template(&mut self, name: N, contents: C) -> Result<(), CustomError> - where - N: Into, - C: AsRef, - { +impl<'a> RendererIntegration<'a> for DusterRenderer<'a> { + fn load_template(&mut self, name: &'a str, contents: &'a str) -> Result<(), CustomError> { let compiled_template = duster::renderer::compile_template(contents.as_ref())?; + self.templates.insert(name, compiled_template); Ok(()) } diff --git a/src/render/renderer_integration.rs b/src/render/renderer_integration.rs index 7d6591d..ebf103c 100644 --- a/src/render/renderer_integration.rs +++ b/src/render/renderer_integration.rs @@ -2,11 +2,8 @@ use serde::Serialize; use crate::error::CustomError; -pub(crate) trait RendererIntegration { - fn load_template(&mut self, name: N, contents: C) -> Result<(), CustomError> - where - N: Into, - C: AsRef; +pub(crate) trait RendererIntegration<'a> { + fn load_template(&mut self, name: &'a str, contents: &'a str) -> Result<(), CustomError>; fn render(&self, context: C) -> Result where