//! Modul für die Konfigurationsverwaltung der Anwendung. //! //! Dieses Modul stellt die Funktionalität zum Laden, Speichern und Verwalten //! der Anwendungskonfiguration bereit. Die Konfiguration wird in einer Datei gespeichert //! und beim Programmstart automatisch geladen. use crate::program; use serde::{Deserialize, Serialize}; use std::sync::OnceLock; /// Hauptkonfigurationsstruktur der Anwendung. /// /// Enthält alle Konfigurationseinstellungen, aufgeteilt in verschiedene Bereiche. #[derive(Serialize, Deserialize, Clone)] pub struct AppConfig { /// Allgemeine Einstellungen pub general: General, /// Lokale Einbindungseinstellungen pub local: Local, /// Remote-Einbindungseinstellungen pub remote: Remote, } impl Default for AppConfig { fn default() -> Self { Self { general: General::default(), local: Local::default(), remote: Remote::default(), } } } /// Allgemeine Konfigurationseinstellungen. #[derive(Serialize, Deserialize, Clone)] pub struct General { /// Log-Level für die Anwendung pub log_level: String, /// Pfad zum Einhängepunkt pub mount_point: String, } impl Default for General { fn default() -> Self { Self { log_level: "info".to_string(), mount_point: get_default_mount_path(), } } } /// Konfiguration für lokale Einbindungen. #[derive(Serialize, Deserialize, Clone)] pub struct Local { /// Typ der lokalen Einbindung (z.B. "nfs") pub mount_type: String, /// Pfad zur lokalen Einbindung pub mount_path: String, /// MAC-Adresse des lokalen Geräts pub device_mac: String, } impl Default for Local { fn default() -> Self { Self { mount_type: "nfs".to_string(), mount_path: "".to_string(), device_mac: "".to_string(), } } } /// Konfiguration für Remote-Einbindungen. #[derive(Serialize, Deserialize, Clone)] pub struct Remote { /// Typ der Remote-Einbindung (z.B. "webdav") pub mount_type: String, /// Pfad zur Remote-Einbindung pub mount_path: String, /// Benutzername für Remote-Zugriff pub username: String, /// Passwort für Remote-Zugriff pub password: String, } impl Default for Remote { fn default() -> Self { Self { mount_type: "webdav".to_string(), mount_path: "".to_string(), username: "".to_string(), password: "".to_string(), } } } /// Ermittelt den Standard-Einhängepunkt basierend auf dem Betriebssystem. fn get_default_mount_path() -> String { #[cfg(not(any(target_os = "windows")))] { "/media/smart_mount".to_string() } #[cfg(any(target_os = "windows"))] { let drives = ('C'..='Z').collect::>(); for drive in drives { let drive_string = format!("{}:", drive); if !std::path::Path::new(&drive_string).exists() { return format!("{}:", drive); } } "H".to_string() } } static CONFIG_NAME: &str = "config"; static CONFIG: OnceLock = OnceLock::new(); /// Gibt die aktuelle Konfiguration zurück. /// /// Lädt die Konfiguration beim ersten Aufruf und speichert sie zwischen. /// Nachfolgende Aufrufe geben die gespeicherte Konfiguration zurück. pub fn get_config() -> &'static AppConfig { CONFIG.get_or_init(|| { load_config() }) } /// Lädt die Konfiguration aus der Konfigurationsdatei. fn load_config() -> AppConfig { confy::load(&*program::program_name(), CONFIG_NAME).unwrap_or_default() } // /// Speichert die übergebene Konfiguration in der Konfigurationsdatei. // fn save_config(config: AppConfig) { // confy::store(&*program::program_name(), CONFIG_NAME, config).unwrap(); // }