diff options
Diffstat (limited to 'server/src/routes/ui/mod.rs')
-rw-r--r-- | server/src/routes/ui/mod.rs | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/server/src/routes/ui/mod.rs b/server/src/routes/ui/mod.rs index 8a3bc4e..e062a68 100644 --- a/server/src/routes/ui/mod.rs +++ b/server/src/routes/ui/mod.rs @@ -1,34 +1,48 @@ -use self::layout::Layout; use markup::Render; use rocket::{ + futures::FutureExt, http::ContentType, response::{self, Responder}, Request, Response, }; -use std::io::Cursor; +use std::{future::Future, io::Cursor, pin::Pin}; +use tokio::io::AsyncRead; +pub mod account; pub mod error; pub mod home; pub mod layout; pub mod node; -pub mod style; pub mod player; -pub mod account; +pub mod style; -pub struct HtmlTemplate<T>(pub String, pub T); +pub struct HtmlTemplate<'a>(pub markup::DynRender<'a>); -impl<'r, T: Render> Responder<'r, 'static> for HtmlTemplate<T> { - fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> { +impl<'r> Responder<'r, 'static> for HtmlTemplate<'_> { + fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { let mut out = String::new(); - Layout { - title: self.0, - main: self.1, - } - .render(&mut out) - .unwrap(); + self.0.render(&mut out).unwrap(); Response::build() .header(ContentType::HTML) .streamed_body(Cursor::new(out)) .ok() } } + +pub struct Defer(Pin<Box<dyn Future<Output = String> + Send>>); + +impl AsyncRead for Defer { + fn poll_read( + mut self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + buf: &mut tokio::io::ReadBuf<'_>, + ) -> std::task::Poll<std::io::Result<()>> { + match self.0.poll_unpin(cx) { + std::task::Poll::Ready(r) => { + buf.put_slice(r.as_bytes()); + std::task::Poll::Ready(Ok(())) + } + std::task::Poll::Pending => std::task::Poll::Pending, + } + } +} |