Style: Automatische Formatierung & Clippy-Fixes
This commit is contained in:
+13
-20
@@ -33,8 +33,8 @@ use std::sync::{OnceLock, RwLock};
|
|||||||
|
|
||||||
pub use confy::ConfyError;
|
pub use confy::ConfyError;
|
||||||
use program_ctdra::try_program_name;
|
use program_ctdra::try_program_name;
|
||||||
use serde::de::DeserializeOwned;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
use sudo_ctdra::is_run_as_root;
|
use sudo_ctdra::is_run_as_root;
|
||||||
|
|
||||||
/// Optionaler benutzerdefinierter Konfigurationsdateiname (Standard: "config").
|
/// Optionaler benutzerdefinierter Konfigurationsdateiname (Standard: "config").
|
||||||
@@ -72,13 +72,11 @@ pub fn set_config_name(name: impl Into<String>) {
|
|||||||
|
|
||||||
/// Liefert den konfigurierten Namen der Konfigurationsdatei (Standard: `"config"`).
|
/// Liefert den konfigurierten Namen der Konfigurationsdatei (Standard: `"config"`).
|
||||||
pub fn get_config_name() -> String {
|
pub fn get_config_name() -> String {
|
||||||
if let Some(lock) = CONFIG_NAME.get() {
|
if let Some(lock) = CONFIG_NAME.get()
|
||||||
if let Ok(guard) = lock.read() {
|
&& let Ok(guard) = lock.read()
|
||||||
if let Some(name) = guard.as_ref() {
|
&& let Some(name) = guard.as_ref() {
|
||||||
return name.clone();
|
return name.clone();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
DEFAULT_CONFIG_NAME.to_string()
|
DEFAULT_CONFIG_NAME.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,11 +98,10 @@ pub fn clear_custom_dir() {
|
|||||||
|
|
||||||
/// Liefert das aktuell gesetzte benutzerdefinierte Konfigurationsverzeichnis, falls vorhanden.
|
/// Liefert das aktuell gesetzte benutzerdefinierte Konfigurationsverzeichnis, falls vorhanden.
|
||||||
pub fn get_custom_dir() -> Option<PathBuf> {
|
pub fn get_custom_dir() -> Option<PathBuf> {
|
||||||
if let Some(lock) = CUSTOM_CONFIG_DIR.get() {
|
if let Some(lock) = CUSTOM_CONFIG_DIR.get()
|
||||||
if let Ok(guard) = lock.read() {
|
&& let Ok(guard) = lock.read() {
|
||||||
return guard.clone();
|
return guard.clone();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,11 +123,10 @@ pub fn clear_custom_path() {
|
|||||||
|
|
||||||
/// Liefert den aktuell gesetzten expliziten Pfad zur Konfigurationsdatei, falls vorhanden.
|
/// Liefert den aktuell gesetzten expliziten Pfad zur Konfigurationsdatei, falls vorhanden.
|
||||||
pub fn get_custom_path() -> Option<PathBuf> {
|
pub fn get_custom_path() -> Option<PathBuf> {
|
||||||
if let Some(lock) = CUSTOM_CONFIG_PATH.get() {
|
if let Some(lock) = CUSTOM_CONFIG_PATH.get()
|
||||||
if let Ok(guard) = lock.read() {
|
&& let Ok(guard) = lock.read() {
|
||||||
return guard.clone();
|
return guard.clone();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,20 +259,17 @@ where
|
|||||||
{
|
{
|
||||||
let map_lock = GLOBAL_CONFIGS.get_or_init(|| RwLock::new(HashMap::new()));
|
let map_lock = GLOBAL_CONFIGS.get_or_init(|| RwLock::new(HashMap::new()));
|
||||||
|
|
||||||
if let Ok(guard) = map_lock.read() {
|
if let Ok(guard) = map_lock.read()
|
||||||
if let Some(entry) = guard.get(&TypeId::of::<T>()) {
|
&& let Some(entry) = guard.get(&TypeId::of::<T>())
|
||||||
if let Some(val) = entry.downcast_ref::<T>() {
|
&& let Some(val) = entry.downcast_ref::<T>() {
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut guard = map_lock.write().unwrap();
|
let mut guard = map_lock.write().unwrap();
|
||||||
if let Some(entry) = guard.get(&TypeId::of::<T>()) {
|
if let Some(entry) = guard.get(&TypeId::of::<T>())
|
||||||
if let Some(val) = entry.downcast_ref::<T>() {
|
&& let Some(val) = entry.downcast_ref::<T>() {
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let loaded: T = load_config::<T>();
|
let loaded: T = load_config::<T>();
|
||||||
let boxed: &'static T = Box::leak(Box::new(loaded));
|
let boxed: &'static T = Box::leak(Box::new(loaded));
|
||||||
|
|||||||
+11
-11
@@ -1,7 +1,7 @@
|
|||||||
use config_ctdra::{
|
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,
|
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 serde::{Deserialize, Serialize};
|
||||||
use std::env;
|
use std::env;
|
||||||
@@ -17,17 +17,11 @@ fn lock_test() -> std::sync::MutexGuard<'static, ()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||||
|
#[derive(Default)]
|
||||||
struct DummyAppConfig {
|
struct DummyAppConfig {
|
||||||
general: DummyGeneral,
|
general: DummyGeneral,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DummyAppConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
general: DummyGeneral::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||||
struct DummyGeneral {
|
struct DummyGeneral {
|
||||||
@@ -199,7 +193,10 @@ fn test_load_config_fallback_and_invalid_toml() {
|
|||||||
|
|
||||||
// load::<T>() sollte bei ungültigem TOML fehlschlagen
|
// load::<T>() sollte bei ungültigem TOML fehlschlagen
|
||||||
let res: Result<CustomServerConfig, ConfyError> = load();
|
let res: Result<CustomServerConfig, ConfyError> = 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::<T>() fällt im Fehlerfall auf Default zurück
|
// load_config::<T>() fällt im Fehlerfall auf Default zurück
|
||||||
let loaded_default: CustomServerConfig = load_config();
|
let loaded_default: CustomServerConfig = load_config();
|
||||||
@@ -346,7 +343,10 @@ fn test_get_config_singleton_cache() {
|
|||||||
|
|
||||||
// Zweiter Aufruf: liefert dieselbe statische Referenz
|
// Zweiter Aufruf: liefert dieselbe statische Referenz
|
||||||
let ref2: &'static DatabaseConfig = get_config();
|
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);
|
cleanup_temp_file(&temp_file);
|
||||||
clear_custom_path();
|
clear_custom_path();
|
||||||
|
|||||||
Reference in New Issue
Block a user