about summary refs log tree commit diff
path: root/src/database/settings.rs
blob: 46346ccea508f9e30cc609f5d6a2c7c28254c347 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
pub use kittybox_frontend_renderer::themes::ThemeName;

mod private {
    pub trait Sealed {}
}

/// A trait for various settings that should be contained here.
///
/// **Note**: this trait is sealed to prevent external
/// implementations, as it wouldn't make sense to add new settings
/// that aren't used by Kittybox itself.
pub trait Setting: private::Sealed + std::fmt::Debug + Default + Clone + serde::Serialize + serde::de::DeserializeOwned + /*From<Settings> +*/ Send + Sync + 'static {
    /// The data that the setting carries.
    type Data: std::fmt::Debug + Send + Sync;
    /// The string ID for the setting, usable as an identifier in the database.
    const ID: &'static str;

    /// Unwrap the setting type, returning owned data contained within.
    fn into_inner(self) -> Self::Data;
    /// Create a new instance of this type containing certain data.
    fn new(data: Self::Data) -> Self;
}

/// A website's title, shown in the header.
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq)]
pub struct SiteName(pub(crate) String);
impl Default for SiteName {
    fn default() -> Self {
        Self("Kittybox".to_string())
    }
}
impl AsRef<str> for SiteName {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}
impl private::Sealed for SiteName {}
impl Setting for SiteName {
    type Data = String;
    const ID: &'static str = "site_name";

    fn into_inner(self) -> String {
        self.0
    }
    fn new(data: Self::Data) -> Self {
        Self(data)
    }
}

/// Participation status in the IndieWeb Webring: https://🕸💍.ws/dashboard
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, Clone, Copy, PartialEq, Eq)]
pub struct Webring(bool);
impl private::Sealed for Webring {}
impl Setting for Webring {
    type Data = bool;
    const ID: &'static str = "webring";

    fn into_inner(self) -> Self::Data {
        self.0
    }

    fn new(data: Self::Data) -> Self {
        Self(data)
    }
}

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq)]
/// Theme setting for Kittybox, specifying the stylesheet used for laying out the website.
pub struct Theme(ThemeName);
impl private::Sealed for Theme {}
impl Setting for Theme {
    type Data = ThemeName;
    const ID: &'static str = "theme";

    fn into_inner(self) -> Self::Data {
        self.0
    }

    fn new(data: Self::Data) -> Self {
        Self(data)
    }
}

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
/// Custom stylesheet for Kittybox, activated by setting the theme to [`ThemeName::Custom`].
pub struct CustomCss(String);
impl private::Sealed for CustomCss {}
impl Setting for CustomCss {
    type Data = String;
    const ID: &'static str = "custom_css";

    fn into_inner(self) -> String {
        self.0
    }

    fn new(data: Self::Data) -> Self {
        Self(data)
    }
}