♻️ (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");
}

View file

@ -1,25 +0,0 @@
use pagetop::prelude::*;
// Settings that claim `[app]`, a section that already belongs to the global settings.
#[settings(section = "app")]
pub struct ClashingSettings {
pub name: String,
}
static SETTINGS: LazySettings<ClashingSettings> = LazySettings::new();
// `EXTENSIONS` is a global `OnceLock`, so this test needs its own test binary.
struct ClashingExtension;
#[async_trait]
impl Extension for ClashingExtension {
fn settings(&self) -> Option<&'static dyn LoadSettings> {
Some(&SETTINGS)
}
}
#[pagetop::test]
#[should_panic(expected = "Settings section [app] is claimed by more than one extension")]
async fn a_section_claimed_twice_stops_the_startup() {
let _ = Application::prepare(&ClashingExtension).await;
}