Skip to main content

configbuilder_pattern_loader/
feature_loader.rs

1use crate::{ConfigError, FeatureState, LoadedFeature};
2
3/// Load optional TOML sections with configurable enable/disable precedence.
4///
5/// Unlike [`Loader`], which returns `T::default()` for absent sections,
6/// `FeatureLoader` distinguishes absent (feature off) from present (feature on)
7/// and enforces a three-level precedence chain:
8///
9/// ```text
10/// env var  >  enabled = false  >  section presence  >  section absent
11/// ```
12///
13/// [`Loader`]: crate::Loader
14pub trait FeatureLoader {
15    /// Load the section at `key` with full metadata.
16    ///
17    /// Applies the full precedence chain — env var override, explicit
18    /// `enabled = false`, section presence — and records the reason in the
19    /// returned [`LoadedFeature::record`].
20    fn load_feature<T>(&self, key: &str) -> Result<LoadedFeature<T>, ConfigError>
21    where
22        T: serde::de::DeserializeOwned;
23
24    /// Load the section at `key` as a `FeatureState`, without metadata.
25    fn load_optional_section<T>(&self, key: &str) -> Result<FeatureState<T>, ConfigError>
26    where
27        T: serde::de::DeserializeOwned,
28    {
29        self.load_feature(key).map(|loaded| loaded.state)
30    }
31}