♻️ (config): Ajustes no válidos sin panic al arrancar

This commit is contained in:
Manuel Cillero 2026-10-13 01:21:34 +02:00
parent 79655932d0
commit fd6030ee7d
6 changed files with 217 additions and 86 deletions

View file

@ -32,8 +32,9 @@ static STARTUP_LOADED: AtomicBool = AtomicBool::new(false);
struct StartupSettings;
impl LoadSettings for StartupSettings {
fn load(&self) {
fn load(&self) -> Result<(), BoxError> {
STARTUP_LOADED.store(true, Ordering::SeqCst);
Ok(())
}
fn section(&self) -> &'static str {
@ -48,6 +49,12 @@ pub struct AbsentSettings {
pub value: u8,
}
// Only one of the keys of `[test]` in `config/test.toml`: the other one is unknown.
#[settings(section = "test")]
pub struct PartialTestSettings {
pub float_value: f32,
}
// `EXTENSIONS` is a global `OnceLock`: all tests in this file share the same root extension.
struct SettingsExtension;
@ -113,3 +120,20 @@ async fn load_from_str_rejects_unknown_keys() {
.to_string();
assert!(error.contains("string_valu"));
}
#[pagetop::test]
async fn load_reports_invalid_settings_instead_of_panicking() {
let settings = LazySettings::<PartialTestSettings>::new();
let error = settings
.load()
.expect_err("unknown key accepted")
.to_string();
assert!(error.contains("string_value"), "{error}");
}
#[pagetop::test]
async fn load_keeps_the_settings_it_reads() {
let settings = LazySettings::<TestSettings>::new();
settings.load().expect("valid settings rejected");
assert_eq!(settings.string_value, "Modified value");
}