summaryrefslogblamecommitdiffstats
path: root/src/ructe_poem.rs
blob: bb2d91375e247369d5439e8120c0e9199993f22c (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                 
                                       


                                                
                                       

















                                                                                        

                      
use poem::{http::StatusCode, Body, IntoResponse};

macro_rules! render {
    ($template:path) => {{
        use $crate::ructe_poem::Render;
        Render(|o| $template(o))
    }};
    ($template:path, $($arg:expr),* $(,)*) => {{
        use $crate::ructe_poem::Render;
        Render(move |o| $template(o, $($arg),*))
    }}
}

pub struct Render<T: FnOnce(&mut Vec<u8>) -> std::io::Result<()> + Send>(pub T);

impl<T: FnOnce(&mut Vec<u8>) -> std::io::Result<()> + Send> IntoResponse for Render<T> {
    fn into_response(self) -> poem::Response {
        let mut buf = Vec::new();
        match self.0(&mut buf) {
            Ok(()) => Body::from_vec(buf).into_response(),
            Err(_e) => {
                // TODO: logging
                (StatusCode::INTERNAL_SERVER_ERROR, "Render failed").into_response()
            }
        }
    }
}

pub(crate) use render;