diff options
Diffstat (limited to 'src/ructe_poem.rs')
-rw-r--r-- | src/ructe_poem.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/ructe_poem.rs b/src/ructe_poem.rs new file mode 100644 index 0000000..fc14048 --- /dev/null +++ b/src/ructe_poem.rs @@ -0,0 +1,27 @@ +use poem::{http::StatusCode, Body, IntoResponse}; + +macro_rules! render { + ($template:path) => {{ + use $crate::axum_ructe::Render; + Render(|o| $template(o)) + }}; + ($template:path, $($arg:expr),* $(,)*) => {{ + use $crate::axum_ructe::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() + } + } + } +} |