I think I have worked around the lifetime issue by keeping references to the intermediate str's.

This commit is contained in:
Tom Alexander 2023-10-22 17:43:30 -04:00
parent 58aba8efd5
commit 043cc5eda4
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 32 additions and 26 deletions

View File

@ -38,12 +38,20 @@ impl SiteRenderer {
return Err("Expect exactly 1 main.dust template file.".into()); return Err("Expect exactly 1 main.dust template file.".into());
} }
for entry in main_template { let decoded_templates = {
load_template_from_include_dir(&mut renderer_integration, entry)?; 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 { for (name, contents) in decoded_templates {
load_template_from_include_dir(&mut renderer_integration, entry)?; renderer_integration.load_template(name, contents)?;
} }
for blog_post in &self.blog_posts { for blog_post in &self.blog_posts {
@ -55,10 +63,9 @@ impl SiteRenderer {
} }
} }
fn load_template_from_include_dir<RI: RendererIntegration>( fn build_name_contents_pairs<'a>(
renderer_integration: &mut RI, entry: &'a include_dir::File<'_>,
entry: &include_dir::File<'_>, ) -> Result<(&'a str, &'a str), CustomError> {
) -> Result<(), CustomError> {
let path = entry.path(); let path = entry.path();
let name = path let name = path
.file_stem() .file_stem()
@ -66,6 +73,5 @@ fn load_template_from_include_dir<RI: RendererIntegration>(
.to_str() .to_str()
.ok_or("All template filenames should be valid utf-8.")?; .ok_or("All template filenames should be valid utf-8.")?;
let contents = std::str::from_utf8(entry.contents())?; let contents = std::str::from_utf8(entry.contents())?;
renderer_integration.load_template(name, contents)?; Ok((name, contents))
Ok(())
} }

View File

@ -1,24 +1,27 @@
use std::collections::HashMap;
use crate::error::CustomError; use crate::error::CustomError;
use super::renderer_integration::RendererIntegration; use super::renderer_integration::RendererIntegration;
use duster::renderer::DustRenderer; use duster::renderer::DustRenderer;
use serde::Serialize; use serde::Serialize;
pub(crate) struct DusterRenderer {} pub(crate) struct DusterRenderer<'a> {
templates: HashMap<&'a str, duster::parser::Template<'a>>,
}
impl DusterRenderer { impl<'a> DusterRenderer<'a> {
pub(crate) fn new() -> DusterRenderer { pub(crate) fn new() -> DusterRenderer<'a> {
DusterRenderer {} DusterRenderer {
templates: HashMap::new(),
}
} }
} }
impl RendererIntegration for DusterRenderer { impl<'a> RendererIntegration<'a> for DusterRenderer<'a> {
fn load_template<N, C>(&mut self, name: N, contents: C) -> Result<(), CustomError> fn load_template(&mut self, name: &'a str, contents: &'a str) -> Result<(), CustomError> {
where
N: Into<String>,
C: AsRef<str>,
{
let compiled_template = duster::renderer::compile_template(contents.as_ref())?; let compiled_template = duster::renderer::compile_template(contents.as_ref())?;
self.templates.insert(name, compiled_template);
Ok(()) Ok(())
} }

View File

@ -2,11 +2,8 @@ use serde::Serialize;
use crate::error::CustomError; use crate::error::CustomError;
pub(crate) trait RendererIntegration { pub(crate) trait RendererIntegration<'a> {
fn load_template<N, C>(&mut self, name: N, contents: C) -> Result<(), CustomError> fn load_template(&mut self, name: &'a str, contents: &'a str) -> Result<(), CustomError>;
where
N: Into<String>,
C: AsRef<str>;
fn render<C>(&self, context: C) -> Result<String, CustomError> fn render<C>(&self, context: C) -> Result<String, CustomError>
where where