summaryrefslogtreecommitdiffstats
path: root/src/ructe_poem.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2024-11-13 20:00:15 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2024-11-13 20:00:15 +0000
commitb7a2265e9b29d8fa09f84f5213ef7f8ed3045ca6 (patch)
tree280a31f5887aafdaa200a2b5f4c05ff106d9e365 /src/ructe_poem.rs
downloadcritch-b7a2265e9b29d8fa09f84f5213ef7f8ed3045ca6.tar.gz
critch-b7a2265e9b29d8fa09f84f5213ef7f8ed3045ca6.tar.bz2
critch-b7a2265e9b29d8fa09f84f5213ef7f8ed3045ca6.zip
initial commit
Diffstat (limited to 'src/ructe_poem.rs')
-rw-r--r--src/ructe_poem.rs27
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()
+ }
+ }
+ }
+}