use serde::{Deserialize, Serialize}; use std::{ net::{IpAddr, Ipv4Addr, SocketAddr}, path::Path, }; const CONFIG_FILE: &str = "config.toml"; #[derive(Deserialize, Serialize)] pub struct Config { //#[serde(default = "Config::default_admin_emails")] //pub admin_emails: Vec, #[serde(default = "Config::default_admin_passwords")] pub admin_passwords: Vec, /// New or edited comments need admin's approval before being public #[serde(default = "Config::default_comment_approve")] pub comment_approve: bool, /// Duration for which a comment can be edited #[serde(default = "Config::default_comment_edit_timeout")] pub comment_edit_timeout: u64, #[serde(default = "Config::default_comment_author_max_len")] pub comment_author_max_len: usize, #[serde(default = "Config::default_comment_email_max_len")] pub comment_email_max_len: usize, #[serde(default = "Config::default_comment_text_max_len")] pub comment_text_max_len: usize, #[serde(default = "Config::default_cookies_https_only")] pub cookies_https_only: bool, #[serde(default = "Config::default_cookies_domain")] pub cookies_domain: Option, #[serde(default = "Config::default_lang")] pub lang: String, #[serde(default = "Config::default_listen")] pub listen: SocketAddr, /// Send a matrix message on new comment #[serde(default = "Config::default_matrix_notify")] pub matrix_notify: bool, #[serde(default = "Config::default_matrix_password")] pub matrix_password: String, #[serde(default = "Config::default_matrix_room")] pub matrix_room: String, #[serde(default = "Config::default_matrix_server")] pub matrix_server: String, #[serde(default = "Config::default_matrix_user")] pub matrix_user: String, #[serde(default = "Config::default_root_url")] pub root_url: String, } impl Config { /*fn default_admin_emails() -> Vec { vec![] }*/ fn default_admin_passwords() -> Vec { vec![] } fn default_comment_approve() -> bool { true } fn default_comment_edit_timeout() -> u64 { 7 * 86400 } fn default_comment_author_max_len() -> usize { 64 } fn default_comment_email_max_len() -> usize { 128 } fn default_comment_text_max_len() -> usize { 128 * 1024 } fn default_cookies_https_only() -> bool { false } fn default_cookies_domain() -> Option { None } fn default_lang() -> String { "en_GB".into() } fn default_listen() -> SocketAddr { SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 31720) } fn default_matrix_notify() -> bool { false } fn default_matrix_password() -> String { "".into() } fn default_matrix_room() -> String { "#maintenance:matrix.txmn.tk".into() } fn default_matrix_server() -> String { "https://matrix.txmn.tk".into() } fn default_matrix_user() -> String { "@tuxmain:matrix.txmn.tk".into() } fn default_root_url() -> String { "/".into() } } impl Default for Config { fn default() -> Self { Self { //admin_emails: Self::default_admin_emails(), admin_passwords: Self::default_admin_passwords(), comment_approve: Self::default_comment_approve(), comment_edit_timeout: Self::default_comment_edit_timeout(), comment_author_max_len: Self::default_comment_author_max_len(), comment_email_max_len: Self::default_comment_email_max_len(), comment_text_max_len: Self::default_comment_text_max_len(), cookies_https_only: Self::default_cookies_https_only(), cookies_domain: Self::default_cookies_domain(), lang: Self::default_lang(), listen: Self::default_listen(), matrix_notify: Self::default_matrix_notify(), matrix_password: Self::default_matrix_password(), matrix_room: Self::default_matrix_room(), matrix_server: Self::default_matrix_server(), matrix_user: Self::default_matrix_user(), root_url: Self::default_root_url(), } } } pub fn read_config(dir: &Path) -> Config { let path = dir.join(CONFIG_FILE); if !path.is_file() { let config = Config::default(); std::fs::write(path, toml_edit::easy::to_string_pretty(&config).unwrap()) .expect("Cannot write config file"); config } else { toml_edit::easy::from_str( std::str::from_utf8(&std::fs::read(path).expect("Cannot read config file")) .expect("Bad encoding in config file"), ) .expect("Bad TOML in config file") } } pub fn write_config(dir: &Path, config: &Config) { let path = dir.join(CONFIG_FILE); std::fs::write(path, toml_edit::easy::to_string_pretty(&config).unwrap()) .expect("Cannot write config file"); }