From: Ian Jackson Date: Sun, 13 Jun 2021 14:23:34 +0000 (+0100) Subject: tera: Make nwtemplates use tera_standalone X-Git-Tag: otter-0.7.2~269 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=f66004a03e8b023ce8df431c7d149417e141da11;p=otter.git tera: Make nwtemplates use tera_standalone Signed-off-by: Ian Jackson --- diff --git a/src/nwtemplates.rs b/src/nwtemplates.rs index 29680a75..42cf490b 100644 --- a/src/nwtemplates.rs +++ b/src/nwtemplates.rs @@ -10,7 +10,7 @@ use parking_lot::{const_rwlock, RwLock, RwLockReadGuard}; static STATE: RwLock> = const_rwlock(None); struct State { - tera: tera::Tera, + tera: tera_standalone::Tera, } #[throws(StartupError)] @@ -20,7 +20,7 @@ pub fn init() { let config = config(); let nwtemplate_dir = &config.nwtemplate_dir; let glob = format!("{}/*.tera", nwtemplate_dir); - let tera = tera::Tera::new(&glob) + let tera = tera_standalone::Tera::new(&glob) .map_err(|e| anyhow!("{}", e)) .context("load tamplates") .with_context(|| nwtemplate_dir.to_string())?; @@ -31,12 +31,19 @@ pub fn init() { } #[throws(anyhow::Error)] -pub fn render(template_name: &str, data: &D) -> String { - fn get_tera() -> MappedRwLockReadGuard<'static, tera::Tera> { +pub fn render(template_name: &str, data: &D) -> String +where D: Serialize + Debug +{ + fn get_tera() -> MappedRwLockReadGuard<'static, tera_standalone::Tera> { let g = STATE.read(); RwLockReadGuard::map(g, |g| &g.as_ref().unwrap().tera) } - get_tera().render(template_name, data).map_err(|e| { + let context = tera_standalone::Context::from_serialize(data) + .with_context( + || format!("failed make context from serializable {:?}", data) + ) + .map_err(IE::from)?; + get_tera().render(template_name, &context).map_err(|e| { error!("template render error: {:?}", &e); anyhow!(e.to_string()) })?