configbuilder-pattern-loader 0.1.0

configbuilder-pattern: loader domain, trait contract + tech-agnostic reference implementation.
Documentation
/// Load a set of optional feature sections in dependency order.
///
/// Computes a topological load order from each type's [`OptionalSection::requires`]
/// declarations and calls [`FeatureRegistryOps::load`] in that order. All loaded
/// values are discarded; use `registry.records()` after the call to inspect results.
///
/// Returns `Err(ConfigError::Validation)` if a dependency cycle is detected.
/// The first load failure propagates and stops further loading.
///
/// # Examples
///
/// ```rust,no_run
/// use configbuilder_pattern_loader::{load_in_order, DefaultLoader, FeatureRegistry, FeatureRegistryOps as _, OptionalSection};
///
/// # #[derive(serde::Deserialize)] struct CacheConfig;
/// # impl OptionalSection for CacheConfig { fn section_name() -> &'static str { "cache" } }
/// # #[derive(serde::Deserialize)] struct BrokerConfig;
/// # impl OptionalSection for BrokerConfig { fn section_name() -> &'static str { "broker" } }
/// # fn real_loader() -> DefaultLoader { unimplemented!() }
/// let loader = real_loader();
/// let mut registry = FeatureRegistry::new();
///
/// load_in_order!(&mut registry, &loader, CacheConfig, BrokerConfig)
///     .expect("dependency order must be acyclic");
///
/// registry.validate_dependencies().expect("all dependencies satisfied");
/// println!("{}", registry.summary());
/// ```
#[macro_export]
macro_rules! load_in_order {
    ($registry:expr, $loader:expr, $($ty:ty),+ $(,)?) => {{
        use $crate::{FeatureRegistryOps as _, OptionalSection as _, TopologyOps as _};

        let _names: &[&str] = &[$(<$ty as OptionalSection>::section_name()),+];
        let _requires: &[&[&str]] = &[$(<$ty as OptionalSection>::requires()),+];

        match $crate::Topology.sort(_names, _requires) {
            Err(_msg) => Err(_msg),
            Ok(_order) => {
                let mut _result: Result<(), $crate::ConfigError> = Ok(());
                'load_loop: for &_idx in &_order {
                    let mut _j: usize = 0;
                    $(
                        if _idx == _j {
                            if let Err(_e) = $registry.load::<$ty>($loader) {
                                _result = Err(_e);
                                break 'load_loop;
                            }
                        }
                        _j += 1;
                    )+
                }
                _result
            }
        }
    }};
}