blob: c4317c4277aac4903565df9c196595c9b4e09ffa (
plain) (
blame)
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
44
45
46
47
48
|
#![forbid(unsafe_code)]
#![deny(elided_lifetimes_in_paths)]
#![deny(unreachable_pub)]
use std::fmt;
use actix_web::body::BoxBody;
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, HttpResponseBuilder, ResponseError};
use askama::mime::extension_to_mime_type;
pub use askama::*;
/// Newtype to let askama::Error implement actix_web::ResponseError.
struct ActixError(Error);
impl fmt::Debug for ActixError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<Error as fmt::Debug>::fmt(&self.0, f)
}
}
impl fmt::Display for ActixError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<Error as fmt::Display>::fmt(&self.0, f)
}
}
impl ResponseError for ActixError {}
pub trait TemplateToResponse {
fn to_response(&self) -> HttpResponse<BoxBody>;
}
impl<T: askama::Template> TemplateToResponse for T {
fn to_response(&self) -> HttpResponse<BoxBody> {
match self.render() {
Ok(buffer) => {
let ctype = extension_to_mime_type(T::EXTENSION.unwrap_or("txt"));
HttpResponseBuilder::new(StatusCode::OK)
.content_type(ctype)
.body(buffer)
}
Err(err) => HttpResponse::from_error(ActixError(err)),
}
}
}
|