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(dir: &str) -> Self {
17 Tera::new(dir).unwrap().into()
18 }
19}
20
21impl From<&String> for TeraHandler {
22 fn from(dir: &String) -> Self {
23 Tera::new(dir).unwrap().into()
24 }
25}
26
27impl From<String> for TeraHandler {
28 fn from(dir: String) -> Self {
29 Tera::new(&dir).unwrap().into()
30 }
31}
32
33impl From<Tera> for TeraHandler {
34 fn from(tera: Tera) -> Self {
35 Self(Arc::new(tera))
36 }
37}
38
39impl From<&[&str]> for TeraHandler {
40 fn from(dir_parts: &[&str]) -> Self {
41 dir_parts.iter().collect::<PathBuf>().into()
42 }
43}
44
45impl TeraHandler {
46 pub fn new(tera: impl Into<Self>) -> Self {
68 tera.into()
69 }
70
71 pub(crate) fn tera(&self) -> &Tera {
72 &self.0
73 }
74}
75
76impl Handler for TeraHandler {
77 async fn run(&self, conn: Conn) -> Conn {
78 conn.with_state(self.clone()).with_state(Context::new())
79 }
80}