♻️ (config): Ajustes con #[settings] al arrancar

- Nuevo atributo `#[settings]`: valores por defecto en los campos (tipos
  comprobados al compilar y documentados); claves desconocidas y valores
  no válidos rechazados. Sustituye a `include_config!`, que se elimina.
- `LazySettings<T>::new()` declara los ajustes de una sección, y
  `Extension::settings()` los carga al arrancar, en orden; un error de
  configuración o una sección reclamada dos veces detiene el arranque.
- Aviso de secciones que no corresponden a ninguna extensión.
- Bootsier: [dev] bootsier_static_dir pasa a [bootsier] static_dir.
This commit is contained in:
Manuel Cillero 2026-10-12 21:41:01 +02:00
parent 8a7a827259
commit 728a5c83dc
32 changed files with 918 additions and 488 deletions

View file

@ -1,40 +1,115 @@
use pagetop::prelude::*;
use serde::Deserialize;
use std::sync::atomic::{AtomicBool, Ordering};
include_config!(SETTINGS: Settings => [
"test.string_value" => "Test String",
"test.int_value" => -321,
"test.float_value" => 2.3456,
]);
// The `testing` *feature* (active with `cargo ts` / `cargo tw`) fixes the "test" run mode at
// compile time, so that `global::SETTINGS` and any other settings automatically load the "test"
// mode configuration from `config/test.toml` (versioned, unlike the `local.*.toml` files).
#[derive(Debug, Deserialize)]
pub struct Settings {
pub test: Test,
}
#[derive(Debug, Deserialize)]
pub struct Test {
#[settings(section = "test")]
pub struct TestSettings {
#[default = "Test String"]
pub string_value: String,
#[default(-321)]
pub int_value: i32,
#[default = 2.3456]
pub float_value: f32,
pub nested: NestedSettings,
}
// The `testing` *feature* (active with `cargo ts` / `cargo tw`) fixes the "test" mode at compile
// time inside `config::CONFIG_VALUES`, so that `global::SETTINGS` and any local `include_config!`
// automatically load the "test" mode configuration from `config/test.toml` (versioned, unlike the
// `local.*.toml` files).
#[settings]
pub struct NestedSettings {
#[default = true]
pub enabled: bool,
}
static SETTINGS: LazySettings<TestSettings> = LazySettings::new();
// Settings only reachable through `Extension::settings()`: `STARTUP_LOADED` can only be set by the
// application loading them at startup.
static STARTUP_LOADED: AtomicBool = AtomicBool::new(false);
struct StartupSettings;
impl LoadSettings for StartupSettings {
fn load(&self) {
STARTUP_LOADED.store(true, Ordering::SeqCst);
}
fn section(&self) -> &'static str {
"test"
}
}
// A section that no configuration file defines.
#[settings(section = "absent")]
pub struct AbsentSettings {
#[default = 7]
pub value: u8,
}
// `EXTENSIONS` is a global `OnceLock`: all tests in this file share the same root extension.
struct SettingsExtension;
#[async_trait]
impl Extension for SettingsExtension {
fn settings(&self) -> Option<&'static dyn LoadSettings> {
Some(&StartupSettings)
}
}
#[pagetop::test]
async fn check_global_config() {
assert_eq!(global::SETTINGS.app.run_mode, "test");
assert_eq!(global::SETTINGS.app.name, "Testing");
assert_eq!(global::SETTINGS.server.bind_port, 9000);
// Not defined in `config/test.toml`: default value.
assert_eq!(global::SETTINGS.server.bind_address, "localhost");
}
#[pagetop::test]
async fn check_local_config() {
assert_eq!(SETTINGS.test.string_value, "Modified value");
assert_eq!(SETTINGS.test.int_value, -321);
assert_eq!(SETTINGS.test.float_value, 8.7654);
async fn files_override_defaults_key_by_key() {
assert_eq!(SETTINGS.string_value, "Modified value");
assert_eq!(SETTINGS.int_value, -321);
assert_eq!(SETTINGS.float_value, 8.7654);
assert!(SETTINGS.nested.enabled);
}
#[pagetop::test]
async fn absent_section_uses_all_defaults() {
static ABSENT: LazySettings<AbsentSettings> = LazySettings::new();
assert_eq!(ABSENT.value, 7);
}
#[pagetop::test]
async fn defaults_come_from_the_fields() {
let defaults = TestSettings::default();
assert_eq!(defaults.string_value, "Test String");
assert_eq!(defaults.int_value, -321);
assert!(defaults.nested.enabled);
}
#[pagetop::test]
async fn unknown_keys_are_rejected() {
// A typo in a key must not be silently ignored.
let result = serde_json::from_str::<TestSettings>(r#"{ "string_valu": "typo" }"#);
let error = result.expect_err("unknown key accepted").to_string();
assert!(error.contains("string_valu"));
let result = serde_json::from_str::<TestSettings>(r#"{ "nested": { "enable": false } }"#);
assert!(result.is_err());
}
#[pagetop::test]
async fn application_loads_settings_at_startup() {
let _ = Application::prepare(&SettingsExtension).await.test();
assert!(STARTUP_LOADED.load(Ordering::SeqCst));
}
#[pagetop::test]
async fn load_from_str_rejects_unknown_keys() {
let error = config::load_from_str::<TestSettings>("[test]\nstring_valu = \"typo\"\n")
.expect_err("unknown key accepted")
.to_string();
assert!(error.contains("string_valu"));
}