Feat: Fügt Config-Modul für die Anwendungskonfiguration hinzu.
This commit is contained in:
130
src/config.rs
Normal file
130
src/config.rs
Normal file
@@ -0,0 +1,130 @@
|
||||
//! 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_path: String,
|
||||
}
|
||||
|
||||
impl Default for General {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
log_level: "info".to_string(),
|
||||
mount_path: get_default_mount_path(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Konfiguration für lokale Einbindungen.
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Local {
|
||||
/// Typ der lokalen Einbindung (z.B. "nfs")
|
||||
pub local_mount_type: String,
|
||||
/// Pfad zur lokalen Einbindung
|
||||
pub local_mount_path: String,
|
||||
/// MAC-Adresse des lokalen Geräts
|
||||
pub local_device_mac: String,
|
||||
}
|
||||
|
||||
impl Default for Local {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
local_mount_type: "nfs".to_string(),
|
||||
local_mount_path: "".to_string(),
|
||||
local_device_mac: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Konfiguration für Remote-Einbindungen.
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Remote {
|
||||
/// Typ der Remote-Einbindung (z.B. "webdav")
|
||||
pub remote_mount_type: String,
|
||||
/// Pfad zur Remote-Einbindung
|
||||
pub remote_mount_path: String,
|
||||
/// Benutzername für Remote-Zugriff
|
||||
pub remote_username: String,
|
||||
/// Passwort für Remote-Zugriff
|
||||
pub remote_password: String,
|
||||
}
|
||||
|
||||
impl Default for Remote {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
remote_mount_type: "webdav".to_string(),
|
||||
remote_mount_path: "".to_string(),
|
||||
remote_username: "".to_string(),
|
||||
remote_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"))]
|
||||
{
|
||||
"".to_string() //TODO Was kommt hier hin????
|
||||
}
|
||||
}
|
||||
|
||||
static CONFIG_NAME: &str = "config";
|
||||
static CONFIG: OnceLock<AppConfig> = 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();
|
||||
}
|
||||
Reference in New Issue
Block a user