aboutsummaryrefslogblamecommitdiffstats
path: root/askama_tide/src/lib.rs
blob: 2854cb6b67315e68f1f729f437ba384fa1622b7a (plain) (tree)
1
2
3
4
                       
                                   
                         
 
























                                                                     
                       
                                                  
                                      



                    
#![forbid(unsafe_code)]
#![deny(elided_lifetimes_in_paths)]
#![deny(unreachable_pub)]

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(error) => {
            let mut response = Response::new(500);
            response.set_error(error);
            response
        }
    }
}