aboutsummaryrefslogtreecommitdiff
path: root/ui/src/lib.rs
blob: 8a4b9507942fa387e8f3b0f74719726514653616 (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
72
73
74
75
76
77
78
79
80
/*
    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) 2025 metamuffin <metamuffin.org>
*/
pub mod filter_sort;
pub mod format;
pub mod home;
pub mod locale;
pub mod node_card;
pub mod node_page;
pub mod props;
pub mod scaffold;
pub mod search;
pub mod settings;
pub mod stats;
pub mod items;
pub mod admin;
pub mod account;

use locale::Language;
use markup::DynRender;
use scaffold::{RenderInfo, Scaffold};
use serde::{Deserialize, Serialize};
use std::{
    path::PathBuf,
    sync::{LazyLock, Mutex},
};

#[rustfmt::skip]
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Config {
    brand: String,
    slogan: String,
    asset_path: PathBuf,
}

static CONF: LazyLock<Config> = LazyLock::new(|| {
    CONF_PRELOAD
        .lock()
        .unwrap()
        .take()
        .expect("cache config not preloaded. logic error")
});
static CONF_PRELOAD: Mutex<Option<Config>> = Mutex::new(None);

/// render as supertrait would be possible but is not
/// dyn compatible and I really dont want to expose generics
/// that generate rendering code because of compile speed.
pub trait Page {
    fn title(&self) -> String;
    fn to_render(&self) -> DynRender;
    fn class(&self) -> Option<&'static str> {
        None
    }
}

pub fn render_page(page: &dyn Page, renderinfo: RenderInfo, lang: Language) -> String {
    Scaffold {
        lang,
        renderinfo,
        class: page.class().unwrap_or("aaaa"),
        title: page.title(),
        main: page.to_render(),
    }
    .to_string()
}

pub struct CustomPage {
    pub title: String,
    pub body: String,
}
impl Page for CustomPage {
    fn title(&self) -> String {
        self.title.clone()
    }
    fn to_render(&self) -> DynRender {
        markup::new!(@markup::raw(&self.body))
    }
}