diff --git a/src/lib.rs b/src/lib.rs index 60f6b3f..9e87599 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,8 +33,8 @@ use std::sync::{OnceLock, RwLock}; pub use confy::ConfyError; use program_ctdra::try_program_name; -use serde::de::DeserializeOwned; use serde::Serialize; +use serde::de::DeserializeOwned; use sudo_ctdra::is_run_as_root; /// Optionaler benutzerdefinierter Konfigurationsdateiname (Standard: "config"). @@ -72,13 +72,11 @@ pub fn set_config_name(name: impl Into) { /// Liefert den konfigurierten Namen der Konfigurationsdatei (Standard: `"config"`). pub fn get_config_name() -> String { - if let Some(lock) = CONFIG_NAME.get() { - if let Ok(guard) = lock.read() { - if let Some(name) = guard.as_ref() { + if let Some(lock) = CONFIG_NAME.get() + && let Ok(guard) = lock.read() + && let Some(name) = guard.as_ref() { return name.clone(); } - } - } DEFAULT_CONFIG_NAME.to_string() } @@ -100,11 +98,10 @@ pub fn clear_custom_dir() { /// Liefert das aktuell gesetzte benutzerdefinierte Konfigurationsverzeichnis, falls vorhanden. pub fn get_custom_dir() -> Option { - if let Some(lock) = CUSTOM_CONFIG_DIR.get() { - if let Ok(guard) = lock.read() { + if let Some(lock) = CUSTOM_CONFIG_DIR.get() + && let Ok(guard) = lock.read() { return guard.clone(); } - } None } @@ -126,11 +123,10 @@ pub fn clear_custom_path() { /// Liefert den aktuell gesetzten expliziten Pfad zur Konfigurationsdatei, falls vorhanden. pub fn get_custom_path() -> Option { - if let Some(lock) = CUSTOM_CONFIG_PATH.get() { - if let Ok(guard) = lock.read() { + if let Some(lock) = CUSTOM_CONFIG_PATH.get() + && let Ok(guard) = lock.read() { return guard.clone(); } - } None } @@ -263,20 +259,17 @@ where { let map_lock = GLOBAL_CONFIGS.get_or_init(|| RwLock::new(HashMap::new())); - if let Ok(guard) = map_lock.read() { - if let Some(entry) = guard.get(&TypeId::of::()) { - if let Some(val) = entry.downcast_ref::() { + if let Ok(guard) = map_lock.read() + && let Some(entry) = guard.get(&TypeId::of::()) + && let Some(val) = entry.downcast_ref::() { return val; } - } - } let mut guard = map_lock.write().unwrap(); - if let Some(entry) = guard.get(&TypeId::of::()) { - if let Some(val) = entry.downcast_ref::() { + if let Some(entry) = guard.get(&TypeId::of::()) + && let Some(val) = entry.downcast_ref::() { return val; } - } let loaded: T = load_config::(); let boxed: &'static T = Box::leak(Box::new(loaded)); diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 2886996..e396141 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1,7 +1,7 @@ use config_ctdra::{ - clear_custom_dir, clear_custom_path, get_config, get_config_name, get_config_path, + ConfyError, clear_custom_dir, clear_custom_path, get_config, get_config_name, get_config_path, get_custom_dir, get_custom_path, get_program_name, load, load_config, modify, modify_config, - save_config, set_config_name, set_custom_dir, set_custom_path, store, ConfyError, + save_config, set_config_name, set_custom_dir, set_custom_path, store, }; use serde::{Deserialize, Serialize}; use std::env; @@ -17,17 +17,11 @@ fn lock_test() -> std::sync::MutexGuard<'static, ()> { } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Default)] struct DummyAppConfig { general: DummyGeneral, } -impl Default for DummyAppConfig { - fn default() -> Self { - Self { - general: DummyGeneral::default(), - } - } -} #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] struct DummyGeneral { @@ -199,7 +193,10 @@ fn test_load_config_fallback_and_invalid_toml() { // load::() sollte bei ungültigem TOML fehlschlagen let res: Result = load(); - assert!(res.is_err(), "Laden von korruptem TOML sollte mit ConfyError fehlschlagen"); + assert!( + res.is_err(), + "Laden von korruptem TOML sollte mit ConfyError fehlschlagen" + ); // load_config::() fällt im Fehlerfall auf Default zurück let loaded_default: CustomServerConfig = load_config(); @@ -346,7 +343,10 @@ fn test_get_config_singleton_cache() { // Zweiter Aufruf: liefert dieselbe statische Referenz let ref2: &'static DatabaseConfig = get_config(); - assert!(std::ptr::eq(ref1, ref2), "get_config muss dieselbe Referenz zurückgeben"); + assert!( + std::ptr::eq(ref1, ref2), + "get_config muss dieselbe Referenz zurückgeben" + ); cleanup_temp_file(&temp_file); clear_custom_path();