diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2022-07-20 21:18:18 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2022-07-25 11:39:58 +0200 |
commit | bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918 (patch) | |
tree | fdca7dca4591f62fca30d798b37779f6fadae00a /askama_hyper/src | |
parent | c5fbd2ebfb0ce84a49db47f09caa12a048ea61d0 (diff) | |
download | askama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.tar.gz askama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.tar.bz2 askama-bd8d2f334ed2a9fd0fb1bb6f9d87b4ab45556918.zip |
Implement basic hyper integration
The integration is based on askama_gotham.
There is no specific trait to convert an arbitrary T to
`hyper::Response`, so I used `From<Template> for hyper::Response`.
Diffstat (limited to 'askama_hyper/src')
-rw-r--r-- | askama_hyper/src/lib.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/askama_hyper/src/lib.rs b/askama_hyper/src/lib.rs new file mode 100644 index 0000000..07ede88 --- /dev/null +++ b/askama_hyper/src/lib.rs @@ -0,0 +1,29 @@ +#![forbid(unsafe_code)] +#![deny(elided_lifetimes_in_paths)] +#![deny(unreachable_pub)] + +pub use askama::*; +#[doc(hidden)] +pub use hyper; +use hyper::{header, Body, Response, StatusCode}; + +pub fn try_respond<T: Template>(t: &T, _ext: &str) -> Result<Response<Body>> { + Response::builder() + .status(StatusCode::OK) + .header( + header::CONTENT_TYPE, + header::HeaderValue::from_static(T::MIME_TYPE), + ) + .body(t.render()?.into()) + .map_err(|err| Error::Custom(Box::new(err))) +} + +pub fn respond<T: Template>(t: &T, _ext: &str) -> Response<Body> { + match try_respond(t, _ext) { + Ok(response) => response, + Err(_) => Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Body::empty()) + .unwrap(), + } +} |