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
//! Handles the global site config file

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use once_cell::sync::OnceCell;
use toml::Value;

static INSTANCE: OnceCell<Config> = OnceCell::new();

// Make sure to update with crate::commands::init::consts
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    pub base_url: String,
    pub compile_katex: bool,
    pub highlight_code: bool,
    pub enable_git: bool,
    pub hl_theme: String,
    pub custom: HashMap<String, Value>,
}

impl Config {
    /// Allows initialization of the global config
    ///
    /// Callable from main.rs only
    #[deny(dead_code)]
    pub(in crate) fn global_init(config: Config) {
        INSTANCE.set(config).unwrap();
    }

    /// Returns a reference to the global config
    ///
    /// Initializes it if it wasn't yet -> useful for tests that don't run main
    pub fn global<'a>() -> &'a Config {
        INSTANCE.get_or_init(|| Config::default())
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            base_url: "/".to_string(),
            compile_katex: true,
            highlight_code: true,
            enable_git: false,
            hl_theme: "InspiredGitHub".to_string(),
            custom: HashMap::new(),
        }
    }
}