use std::{
env,
fs,
path::{Path, PathBuf},
};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub overlay_visible: bool,
pub hotkey: String,
pub ui_language: Option<String>,
pub active_profile: Option<String>,
pub profiles: Vec<Profile>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Profile {
pub name: String,
pub description: String,
pub groups: Vec<ProcessGroup>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProcessGroup {
pub name: String,
pub targets: Vec<ProcessTarget>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProcessTarget {
pub display_name: String,
pub process_name: String,
pub pid: Option<u32>,
pub manual: bool,
}
impl Default for Settings {
fn default() -> Self {
Self {
overlay_visible: true,
hotkey: "Ctrl+Shift+R".to_string(),
ui_language: None,
active_profile: None,
profiles: Vec::new(),
}
}
}
impl Settings {
pub fn load_or_default(path: &Path) -> Result<Self> {
if !path.exists() {
let settings = Self::default();
settings.save(path)?;
return Ok(settings);
}
let content = fs::read_to_string(path)
.with_context(|| format!("Konnte Datei {} nicht lesen", path.display()))?;
let settings = toml::from_str::<Self>(&content)
.with_context(|| format!("Konnte TOML in {} nicht parsen", path.display()))?;
Ok(settings)
}
pub fn save(&self, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).with_context(|| {
format!("Konnte Konfigurationsverzeichnis {} nicht anlegen", parent.display())
})?;
}
let serialized = toml::to_string_pretty(self).context("Konnte Settings nicht serialisieren")?;
fs::write(path, serialized)
.with_context(|| format!("Konnte Datei {} nicht schreiben", path.display()))
}
pub fn active_profile_index(&self) -> Option<usize> {
self.active_profile
.as_ref()
.and_then(|name| self.profiles.iter().position(|p| &p.name == name))
}
pub fn set_active_profile_by_index(&mut self, index: Option<usize>) {
self.active_profile = index.and_then(|i| self.profiles.get(i)).map(|p| p.name.clone());
}
}
pub fn settings_path() -> Result<PathBuf> {
let config_dir = platform_config_dir()?;
Ok(config_dir.join("durin").join("settings.toml"))
}
#[cfg(windows)]
fn platform_config_dir() -> Result<PathBuf> {
env::var_os("APPDATA")
.map(PathBuf::from)
.context("APPDATA ist nicht gesetzt; Konfigurationspfad kann nicht bestimmt werden")
}
#[cfg(target_os = "linux")]
fn platform_config_dir() -> Result<PathBuf> {
if let Some(xdg_config_home) = env::var_os("XDG_CONFIG_HOME") {
return Ok(PathBuf::from(xdg_config_home));
}
let home = env::var_os("HOME")
.map(PathBuf::from)
.context("HOME ist nicht gesetzt; Konfigurationspfad kann nicht bestimmt werden")?;
Ok(home.join(".config"))
}
#[cfg(not(any(windows, target_os = "linux")))]
fn platform_config_dir() -> Result<PathBuf> {
std::env::current_dir().context("Aktuelles Arbeitsverzeichnis konnte nicht bestimmt werden")
}