use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; use std::fs; use std::path::PathBuf; const DEFAULT_PATH: &str = "/etc/mountaineer/sysop.toml"; pub fn path() -> PathBuf { std::env::var_os("SYSOP_CONFIG") .map(PathBuf::from) .unwrap_or_else(|| PathBuf::from(DEFAULT_PATH)) } #[derive(Debug, Default, Serialize, Deserialize)] pub struct Config { #[serde(default)] pub motd: Motd, } #[derive(Debug, Serialize, Deserialize)] pub struct Motd { pub enabled: bool, } impl Default for Motd { fn default() -> Self { Self { enabled: true } } } pub fn load() -> Result { let p = path(); if !p.exists() { return Ok(Config::default()); } let s = fs::read_to_string(&p).with_context(|| format!("read {}", p.display()))?; toml::from_str(&s).with_context(|| format!("parse {}", p.display())) } pub fn save(cfg: &Config) -> Result<()> { let p = path(); if let Some(dir) = p.parent() { fs::create_dir_all(dir).with_context(|| format!("create {}", dir.display()))?; } let s = toml::to_string_pretty(cfg).context("serialize config")?; fs::write(&p, s).with_context(|| format!("write {}", p.display())) }