Skip to main content

axum_template/engine/
tera.rs

1use crate::TemplateEngine;
2
3use super::Engine;
4
5use axum::{http::StatusCode, response::IntoResponse};
6use tera::{Context, Tera};
7use thiserror::Error;
8
9impl TemplateEngine for Engine<Tera> {
10    type Error = TeraError;
11
12    fn render<D: serde::Serialize>(&self, key: &str, data: D) -> Result<String, Self::Error> {
13        let data = Context::from_serialize(data)?;
14        let rendered = self.engine.render(key, &data)?;
15
16        Ok(rendered)
17    }
18}
19
20/// Error wrapper for [`tera::Error`]
21#[derive(Error, Debug)]
22pub enum TeraError {
23    /// See [`tera::Error`]
24    #[error(transparent)]
25    RenderError(#[from] tera::Error),
26}
27
28impl IntoResponse for TeraError {
29    fn into_response(self) -> axum::response::Response {
30        (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
31    }
32}