aboutsummaryrefslogtreecommitdiffstats
path: root/askama_tide/src/lib.rs
diff options
context:
space:
mode:
authorLibravatar Jacob Rothstein <hi@jbr.me>2020-07-09 21:06:03 -0700
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2020-07-14 09:46:12 +0200
commit29cb90bceb59d9dbea94342dfdd8136e1ef494e6 (patch)
tree8d4d63527fbaa039ccf7f0982ff8849c238e68d3 /askama_tide/src/lib.rs
parent0a6284aa91e1eb8da81d7b317fb539653e519d0a (diff)
downloadaskama-29cb90bceb59d9dbea94342dfdd8136e1ef494e6.tar.gz
askama-29cb90bceb59d9dbea94342dfdd8136e1ef494e6.tar.bz2
askama-29cb90bceb59d9dbea94342dfdd8136e1ef494e6.zip
askama tide
Diffstat (limited to 'askama_tide/src/lib.rs')
-rw-r--r--askama_tide/src/lib.rs32
1 files changed, 32 insertions, 0 deletions
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: Template>(t: &T, ext: &str) -> Result<Body> {
+ 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: Template>(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
+ }
+ }
+}