From be8aa4a112bf8df4ba27b4ebbf78e87f49c476f4 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 28 Mar 2022 01:07:37 +0100 Subject: [PATCH] web templates: Provide our own directory scanner We don't know exactly what Tera::new() does, but in any case we want to do something it doesn't support: have template names which do not contain the .tera file extension. No functional change in this commit. Signed-off-by: Ian Jackson --- daemon/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/daemon/main.rs b/daemon/main.rs index 63e93402..bd2b6828 100644 --- a/daemon/main.rs +++ b/daemon/main.rs @@ -69,8 +69,19 @@ pub struct Templates { impl Templates { #[throws(StartupError)] pub fn new(template_dir: &str) -> Self { - let tera = Tera::new(&format!("{}/*.tera", template_dir)) - .context("initialise templates")?; + let mut tera = Tera::default(); + (||{ + let files: Vec<_> = + fs::read_dir(template_dir).context("open directory")? + .filter_map_ok(|entry| { + let leaf = entry.file_name().into_string().ok()?; + leaf.ends_with(".tera").then(||())?; + Some((entry.path(), Some(leaf))) + }) + .try_collect().context("read directory")?; + tera.add_template_files(files).context("process templates")?; + Ok::<_,StartupError>(()) + })().with_context(|| format!("{:?}", template_dir))?; Templates { tera } } -- 2.30.2