I think I have worked around the lifetime issue by keeping references to the intermediate str's.
This commit is contained in:
		
							parent
							
								
									58aba8efd5
								
							
						
					
					
						commit
						043cc5eda4
					
				| @ -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<RI: RendererIntegration>( | ||||
|     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<RI: RendererIntegration>( | ||||
|         .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)) | ||||
| } | ||||
|  | ||||
| @ -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<N, C>(&mut self, name: N, contents: C) -> Result<(), CustomError> | ||||
|     where | ||||
|         N: Into<String>, | ||||
|         C: AsRef<str>, | ||||
|     { | ||||
| 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(()) | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -2,11 +2,8 @@ use serde::Serialize; | ||||
| 
 | ||||
| use crate::error::CustomError; | ||||
| 
 | ||||
| pub(crate) trait RendererIntegration { | ||||
|     fn load_template<N, C>(&mut self, name: N, contents: C) -> Result<(), CustomError> | ||||
|     where | ||||
|         N: Into<String>, | ||||
|         C: AsRef<str>; | ||||
| pub(crate) trait RendererIntegration<'a> { | ||||
|     fn load_template(&mut self, name: &'a str, contents: &'a str) -> Result<(), CustomError>; | ||||
| 
 | ||||
|     fn render<C>(&self, context: C) -> Result<String, CustomError> | ||||
|     where | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander