summaryrefslogtreecommitdiff
path: root/src/layout.rs
blob: 3906b6fa2a3343536c8c4656be3721099e1bc499 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
    This file is part of metamuffins website (https://codeberg.org/metamuffin/website)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
use crate::pages::*;
use crate::uri;
use markup::Render;
use rocket::{
    http::ContentType,
    response::{self, Responder},
    Request, Response,
};
use std::io::Cursor;

markup::define! {
    Layout<Main: Render>(title: String, main: Main) {
        @markup::doctype()
        html {
            head {
                title { @title " - " "metamuffin's website" }
            }
            body {
                img[src="https://s.metamuffin.org/avatar/default-512.webp", align="left", height=80, hspace=10];
                h1 { "metamuffin's personal website" }
                nav {
                    a[href=uri!(r_about())] { "About" } " "
                    a[href=uri!(r_projects())] { "Projects" } " "
                    a[href=uri!(r_contact())] { "Contact" } " "
                    a[href="https://codeberg.org/metamuffin"] { "Codeberg" } " "
                }
                hr;
                section { @main }
                hr;
                footer {
                    p {
                        "metamuffin's website; "
                        "sources available on " a[href="https://codeberg.org/metamuffin/website"] { "codeberg" }
                    }
                }
            }
        }
    }
}

pub type DynLayoutPage<'a> = LayoutPage<markup::DynRender<'a>>;

pub struct LayoutPage<T> {
    pub title: String,
    pub content: T,
}

impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> {
    fn respond_to(self, _req: &'r Request<'_>) -> response::Result<'static> {
        // TODO blocking the event loop here. it seems like there is no other way to
        // TODO offload this, since the guard references `req` which has a lifetime.
        // TODO therefore we just block. that is fine since the database is somewhat fast.
        let mut out = String::new();
        Layout {
            main: self.content,
            title: self.title,
        }
        .render(&mut out)
        .unwrap();

        Response::build()
            .header(ContentType::HTML)
            .streamed_body(Cursor::new(out))
            .ok()
    }
}