trillium_tera/
tera_handler.rs1use std::{path::PathBuf, sync::Arc};
2use tera::{Context, Tera};
3use trillium::{Conn, Handler};
4
5#[derive(Clone, Debug)]
7pub struct TeraHandler(Arc<Tera>);
8
9impl From<PathBuf> for TeraHandler {
10 fn from(dir: PathBuf) -> Self {
11 dir.to_str().unwrap().into()
12 }
13}
14
15impl From<&str> for TeraHandler {
16 fn from(glob: &str) -> Self {
17 let mut tera = Tera::new();
18 tera.load_from_glob(glob).unwrap();
19 tera.into()
20 }
21}
22
23impl From<&String> for TeraHandler {
24 fn from(glob: &String) -> Self {
25 glob.as_str().into()
26 }
27}
28
29impl From<String> for TeraHandler {
30 fn from(glob: String) -> Self {
31 glob.as_str().into()
32 }
33}
34
35impl From<Tera> for TeraHandler {
36 fn from(tera: Tera) -> Self {
37 Self(Arc::new(tera))
38 }
39}
40
41impl From<&[&str]> for TeraHandler {
42 fn from(dir_parts: &[&str]) -> Self {
43 dir_parts.iter().collect::<PathBuf>().into()
44 }
45}
46
47impl TeraHandler {
48 pub fn new(tera: impl Into<Self>) -> Self {
70 tera.into()
71 }
72
73 pub(crate) fn tera(&self) -> &Tera {
74 &self.0
75 }
76}
77
78impl Handler for TeraHandler {
79 async fn run(&self, conn: Conn) -> Conn {
80 conn.with_state(self.clone()).with_state(Context::new())
81 }
82}