credence_lib/render/templates/
templates.rs1use super::{super::super::configuration::*, filters::*};
2
3use {
4 ::axum::http::*,
5 kutil::{http::*, std::immutable::*},
6 minijinja::{Error, *},
7 serde::*,
8 std::{path::*, result::Result},
9};
10
11const DEFAULT_TEMPLATE_CONTENT: &str = include_str!("default.jinja");
12
13pub const DEFAULT_DATE_TIME_FORMAT: &str = "[month repr:long] [day padding:none], [year]";
15
16#[derive(Clone, Debug)]
22pub struct Templates {
23 pub jinja_environment: Environment<'static>,
25}
26
27impl Templates {
28 pub fn new(configuration: &FilesConfiguration) -> Self {
30 let mut jinja_environment = Environment::new(); jinja_environment.set_loader(path_loader_with_default(&configuration.templates));
32 jinja_environment.set_keep_trailing_newline(true);
33 jinja_environment.set_lstrip_blocks(true);
34 jinja_environment.set_trim_blocks(true);
35
36 jinja_environment.add_global("DATE_FORMAT", DEFAULT_DATE_TIME_FORMAT);
37 minijinja_contrib::add_to_environment(&mut jinja_environment);
38
39 jinja_environment.add_global("ASSETS_PATH", configuration.assets.to_string_lossy());
40 jinja_environment.add_filter("fileversion", fileversion_filter);
41
42 jinja_environment.add_filter("parentpath", parentpath_filter);
43
44 Self { jinja_environment }
45 }
46
47 pub async fn render<ContextT>(&self, template_name: &str, context: ContextT) -> Result<ByteString, StatusCode>
49 where
50 ContextT: Serialize,
51 {
52 let template = self.jinja_environment.get_template(template_name).map_err_internal_server("get template")?;
53 template.render(context).map(|string| string.into()).map_err_internal_server("render template")
54 }
55}
56
57fn path_loader_with_default<'path, PathT>(
58 path: PathT,
59) -> impl Fn(&str) -> Result<Option<String>, Error> + 'static + Send + Sync
60where
61 PathT: AsRef<Path> + 'path,
62{
63 let loader = path_loader(path);
64 move |path| {
65 loader(path).map(|template| {
66 template.or_else(|| if path == DEFAULT_TEMPLATE { Some(DEFAULT_TEMPLATE_CONTENT.into()) } else { None })
67 })
68 }
69}