53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct AppConfig {
|
|
pub input_device: String,
|
|
pub output_device: String,
|
|
pub noise_gate_threshold: f32,
|
|
}
|
|
|
|
impl Default for AppConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
input_device: "".to_string(),
|
|
output_device: "".to_string(),
|
|
noise_gate_threshold: 0.01,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AppConfig {
|
|
fn config_path() -> Option<PathBuf> {
|
|
dirs::config_dir().map(|mut p| {
|
|
p.push("peerspeak");
|
|
p.push("config.json");
|
|
p
|
|
})
|
|
}
|
|
|
|
pub fn load() -> Self {
|
|
if let Some(path) = Self::config_path() {
|
|
if let Ok(contents) = fs::read_to_string(&path) {
|
|
if let Ok(config) = serde_json::from_str(&contents) {
|
|
return config;
|
|
}
|
|
}
|
|
}
|
|
Self::default()
|
|
}
|
|
|
|
pub fn save(&self) {
|
|
if let Some(path) = Self::config_path() {
|
|
if let Some(dir) = path.parent() {
|
|
let _ = fs::create_dir_all(dir);
|
|
}
|
|
if let Ok(json) = serde_json::to_string_pretty(self) {
|
|
let _ = fs::write(path, json);
|
|
}
|
|
}
|
|
}
|
|
}
|