Skip to main content

configbuilder_pattern_loader/
config_section.rs

1use crate::{ConfigError, DefaultLoader};
2
3/// Marks a typed struct as the owner of a named TOML section.
4///
5/// Implement this on any `serde::Deserialize + Default` config struct. The
6/// runtime calls [`ConfigSection::load`] once at startup — callers never
7/// write the loader call manually.
8pub trait ConfigSection: serde::de::DeserializeOwned + Default + Send + Sync + 'static {
9    /// The top-level TOML key for this section (e.g. `"mtls"`, `"authz"`).
10    fn section_name() -> &'static str;
11
12    /// The top-level TOML key for this section from a runtime value.
13    fn section_key(&self) -> &'static str {
14        Self::section_name()
15    }
16
17    /// Load this section from `loader`, returning `Self::default()` when the
18    /// key is absent. Override only when custom merge logic is required.
19    fn load(loader: &DefaultLoader) -> Result<Self, ConfigError> {
20        loader.load_section(Self::section_name())
21    }
22}