use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
#[serde(default)]
pub server: ServerConfig,
#[serde(default)]
pub tts: TtsConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TtsConfig {
pub enabled: bool,
pub voice_id: Option<String>,
}
impl Config {
pub fn save(&self, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).ok();
}
let text = toml::to_string_pretty(self)?;
fs::write(path, text)?;
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
pub port: u16,
pub bind: String,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
port: 7878,
bind: "127.0.0.1".into(),
}
}
}
impl Config {
pub fn load_or_default(path: &Path) -> Result<Self> {
if !path.exists() {
return Ok(Self::default());
}
let text = fs::read_to_string(path)?;
Ok(toml::from_str(&text)?)
}
}