use crate::error::ConfigError;
use crate::DEFAULT_PORT;
use log::*;
use serde::{Deserialize, Serialize};
use std::fs;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Config {
pub allow_invalid_server_cert: Option<bool>,
#[serde(default)]
pub audio: Option<AudioConfig>,
pub servers: Option<Vec<ServerConfig>>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct SoundEffect {
pub event: String,
pub file: String,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AudioConfig {
pub input_volume: Option<f32>,
pub output_volume: Option<f32>,
pub sound_effects: Option<Vec<SoundEffect>>,
pub disable_noise_gate: Option<bool>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerConfig {
pub name: String,
pub host: String,
pub port: Option<u16>,
pub username: Option<String>,
pub password: Option<String>,
pub accept_invalid_cert: Option<bool>,
}
impl ServerConfig {
pub fn to_socket_addr(&self) -> Option<SocketAddr> {
(self.host.as_str(), self.port.unwrap_or(DEFAULT_PORT))
.to_socket_addrs()
.ok()?
.next()
}
}
pub fn default_cfg_path() -> PathBuf {
match dirs::config_dir() {
Some(mut p) => {
p.push("mumdrc");
p
}
None => PathBuf::from("/etc/mumdrc"),
}
}
pub fn read_cfg(path: &Path) -> Result<Config, ConfigError> {
match fs::read_to_string(path) {
Ok(s) => Ok(toml_edit::de::from_str(&s)?),
Err(e) => {
if matches!(e.kind(), std::io::ErrorKind::NotFound) && !path.exists() {
warn!("Config file not found");
} else {
error!("Error reading config file: {}", e);
}
Ok(Config::default())
}
}
}