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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#![doc(html_root_url = "https://docs.rs/tsukuyomi-askama/0.1.0")]
#![warn(
missing_debug_implementations,
nonstandard_style,
rust_2018_idioms,
rust_2018_compatibility,
unused
)]
#![cfg_attr(tsukuyomi_deny_warnings, deny(warnings))]
#![cfg_attr(tsukuyomi_deny_warnings, doc(test(attr(deny(warnings)))))]
#![cfg_attr(feature = "cargo-clippy", warn(pedantic))]
#![cfg_attr(feature = "cargo-clippy", forbid(unimplemented))]
extern crate askama;
extern crate http;
extern crate mime_guess;
extern crate tsukuyomi;
use {
askama::Template,
http::{
header::{HeaderValue, CONTENT_TYPE},
Response,
},
mime_guess::get_mime_type_str,
tsukuyomi::{
error::{internal_server_error, Result},
input::Input,
},
};
#[inline]
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub fn respond_to<T>(t: T, _: &mut Input<'_>) -> Result<Response<String>>
where
T: Template,
{
let content_type = t
.extension()
.and_then(get_mime_type_str)
.unwrap_or("text/html; charset=utf-8");
let mut response = t
.render()
.map(Response::new)
.map_err(internal_server_error)?;
response
.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static(content_type));
Ok(response)
}