aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/mod.rs
blob: d0636c52bbd624f030e6516d3aeb8251de4f8fdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
use markup::Render;
use rocket::{
    futures::FutureExt,
    http::ContentType,
    response::{self, Responder},
    Request, Response,
};
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 player;
pub mod style;

pub struct HtmlTemplate<'a>(pub markup::DynRender<'a>);

impl<'r> Responder<'r, 'static> for HtmlTemplate<'_> {
    fn respond_to(self, _req: &'r Request<'_>) -> response::Result<'static> {
        let mut out = String::new();
        self.0.render(&mut out).unwrap();
        Response::build()
            .header(ContentType::HTML)
            .sized_body(out.as_bytes().len(), 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,
        }
    }
}