1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::prelude::*;
use parking_lot::MappedRwLockReadGuard;
use parking_lot::{const_rwlock, RwLock, RwLockReadGuard};
static STATE: RwLock<Option<State>> = const_rwlock(None);
struct State {
tera: tera::Tera,
}
#[throws(StartupError)]
pub fn init() {
let mut guard = STATE.write();
assert!(guard.is_none());
let config = config();
let nwtemplate_dir = &config.nwtemplate_dir;
let glob = format!("{}/*.tera", nwtemplate_dir);
let tera = tera::Tera::new(&glob)
.map_err(|e| anyhow!("{}", e))
.context("load tamplates")
.with_context(|| nwtemplate_dir.to_string())?;
*guard = Some(State {
tera,
})
}
#[throws(anyhow::Error)]
pub fn render<D: Serialize>(template_name: &str, data: &D) -> String {
fn get_tera() -> MappedRwLockReadGuard<'static, tera::Tera> {
let g = STATE.read();
RwLockReadGuard::map(g, |g| &g.as_ref().unwrap().tera)
}
get_tera().render(template_name, data).map_err(|e| {
error!("template render error: {:?}", &e);
anyhow!(e.to_string())
})?
}