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