From: Ian Jackson Date: Mon, 28 Mar 2022 00:07:37 +0000 (+0100) Subject: web templates: Provide our own directory scanner X-Git-Tag: otter-1.0.0~98 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=be8aa4a112bf8df4ba27b4ebbf78e87f49c476f4;p=otter.git 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 --- 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 } }