grace/htmlua

HTML Templating Engine

git clone https://git.gae.moe/grace/htmlua

gyoderfixed parser bugbfa5d30

render_fixes
3.3 KiB100 linesraw
1use std::{fs, path::PathBuf};
2
3use anyhow::{Context, Result};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Deserialize, Serialize, Clone)]
7pub struct Config {
8    pub paths: PathConfig,
9    pub server: ServerConfig,
10    pub syntax_highlighting: SyntaxConfig,
11}
12
13#[derive(Debug, Deserialize, Serialize, Clone)]
14pub struct PathConfig {
15    pub pages: PathBuf,
16    pub components: PathBuf,
17    pub themes: PathBuf,
18}
19
20#[derive(Debug, Deserialize, Serialize, Clone)]
21pub struct ServerConfig {
22    pub host: String,
23    pub port: u16,
24}
25
26#[derive(Debug, Deserialize, Serialize, Clone)]
27pub struct SyntaxConfig {
28    pub default_theme: String,
29    pub load_custom_themes: bool,
30}
31
32impl Default for Config {
33    fn default() -> Self {
34        Self {
35            paths: PathConfig {
36                pages: PathBuf::from("/var/www/htmlua/pages"),
37                components: PathBuf::from("/var/www/htmlua/components"),
38                themes: PathBuf::from("/var/www/htmlua/themes"),
39            },
40            server: ServerConfig {
41                host: "127.0.0.1".to_string(),
42                port: 8080,
43            },
44            syntax_highlighting: SyntaxConfig {
45                default_theme: "base16-ocean.dark".to_string(),
46                load_custom_themes: true,
47            },
48        }
49    }
50}
51
52impl Config {
53    pub fn load() -> Result<Self> {
54        let config_path = Self::get_config_path();
55        if config_path.exists() {
56            let config_content = fs::read_to_string(&config_path)
57                .with_context(|| format!("Failed to read config file: {}", config_path.display()))?;
58            let config: Config = toml::from_str(&config_content)
59                .with_context(|| format!("Failed to parse config file: {}", config_path.display()))?;
60            Ok(config)
61        } else {
62            let default_config = Config::default();
63            if let Some(parent) = config_path.parent() {
64                fs::create_dir_all(parent)
65                    .with_context(|| format!("Failed to create config directory: {}", parent.display()))?;
66            }
67            let config_content =
68                toml::to_string_pretty(&default_config).context("Failed to serialize default config")?;
69            fs::write(&config_path, config_content)
70                .with_context(|| format!("Failed to write default config to: {}", config_path.display()))?;
71            Ok(default_config)
72        }
73    }
74
75    fn get_config_path() -> PathBuf {
76        if cfg!(windows) {
77            dirs::config_dir()
78                .unwrap_or_else(|| PathBuf::from("C:\\ProgramData"))
79                .join("htmlua")
80                .join("config.toml")
81        } else {
82            PathBuf::from("/etc/htmlua.toml")
83        }
84    }
85
86    pub fn save(&self) -> Result<()> {
87        let config_path = Self::get_config_path();
88        if let Some(parent) = config_path.parent() {
89            fs::create_dir_all(parent)
90                .with_context(|| format!("Failed to create config directory: {}", parent.display()))?;
91        }
92        let config_content = toml::to_string_pretty(self).context("Failed to serialize config")?;
93        fs::write(&config_path, config_content)
94            .with_context(|| format!("Failed to write config to: {}", config_path.display()))?;
95        Ok(())
96    }
97
98    #[must_use]
99    pub fn config_file_path() -> PathBuf { Self::get_config_path() }
100}