From 29cb90bceb59d9dbea94342dfdd8136e1ef494e6 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Thu, 9 Jul 2020 21:06:03 -0700 Subject: askama tide --- askama_tide/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 askama_tide/src/lib.rs (limited to 'askama_tide/src') diff --git a/askama_tide/src/lib.rs b/askama_tide/src/lib.rs new file mode 100644 index 0000000..0458348 --- /dev/null +++ b/askama_tide/src/lib.rs @@ -0,0 +1,32 @@ +pub use askama; +pub use tide; + +use askama::*; +use tide::{http::Mime, Body, Response}; + +pub fn try_into_body(t: &T, ext: &str) -> Result { + let string = t.render()?; + let mut body = Body::from_string(string); + + if let Some(mime) = Mime::from_extension(ext) { + body.set_mime(mime); + } + + Ok(body) +} + +pub fn into_response(t: &T, ext: &str) -> Response { + match try_into_body(t, ext) { + Ok(body) => { + let mut response = Response::new(200); + response.set_body(body); + response + } + + Err(e) => { + let mut response = Response::new(500); + response.set_body(e.to_string()); + response + } + } +} -- cgit