diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-12-01 06:43:36 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-01 15:43:36 +0100 |
commit | e3d690ba102f3624ff65f5476dc68dfe98f2e221 (patch) | |
tree | 873c6957e727befe1103633a3b5559451e66f743 /askama_mendes/src/lib.rs | |
parent | 671bc7f32fd150f04793d8d5dbe806e9d4cfe669 (diff) | |
download | askama-e3d690ba102f3624ff65f5476dc68dfe98f2e221.tar.gz askama-e3d690ba102f3624ff65f5476dc68dfe98f2e221.tar.bz2 askama-e3d690ba102f3624ff65f5476dc68dfe98f2e221.zip |
Move askama_mendes integration into Askama repo (#561)
Diffstat (limited to 'askama_mendes/src/lib.rs')
-rw-r--r-- | askama_mendes/src/lib.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/askama_mendes/src/lib.rs b/askama_mendes/src/lib.rs new file mode 100644 index 0000000..8f32269 --- /dev/null +++ b/askama_mendes/src/lib.rs @@ -0,0 +1,39 @@ +#![deny(elided_lifetimes_in_paths)] + +use std::convert::TryFrom; + +use mendes::application::{Application, Responder}; +use mendes::http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE}; +use mendes::http::request::Parts; +use mendes::http::Response; +use mime_guess::MimeGuess; + +pub use askama::*; + +pub fn into_response<A, T>( + app: &A, + req: &Parts, + t: &T, + ext: Option<&str>, +) -> Response<A::ResponseBody> +where + A: Application, + T: Template, + A::ResponseBody: From<String>, + A::Error: From<askama::Error>, +{ + let content = match t.render() { + Ok(content) => content, + Err(e) => return <A::Error as From<_>>::from(e).into_response(app, req), + }; + + let mut builder = Response::builder(); + builder = builder.header(CONTENT_LENGTH, content.len()); + if let Some(ext) = ext { + if let Some(ty) = MimeGuess::from_ext(ext).first() { + builder = builder.header(CONTENT_TYPE, HeaderValue::try_from(ty.as_ref()).unwrap()); + } + } + + builder.body(content.into()).unwrap() +} |