Skip to main content

configbuilder_pattern_loader/
loader.rs

1use crate::ConfigError;
2
3/// Load typed TOML sections from a layered config chain.
4///
5/// Implementations merge config directories in order (later wins) and return
6/// `Ok(T::default())` when the requested key is absent from every source.
7/// Environment variable substitution (`{{VAR_NAME}}` syntax) is optionally supported
8/// when the loader was created with a `SubstitutionPolicy`.
9///
10/// The reference implementation is [`DefaultLoader`]. This trait is in scope for
11/// generic code that accepts any loader.
12///
13/// [`DefaultLoader`]: crate::DefaultLoader
14pub trait Loader {
15    /// Load the section at `key` (dotted path, e.g. `"outer.inner"`) from all
16    /// configured directories, merging with last-wins semantics.
17    ///
18    /// Returns `Ok(T::default())` when the key is absent from every file.
19    /// If the loader was created with a substitution policy, `{{VAR_NAME}}`
20    /// placeholders are substituted with environment variable values after loading.
21    fn load_section<T>(&self, key: &str) -> Result<T, ConfigError>
22    where
23        T: serde::de::DeserializeOwned + Default;
24
25    /// Validate that all configured directories are accessible.
26    ///
27    /// Non-existent paths are permitted (skipped at load time); a path that
28    /// exists but is not a directory is a hard error.
29    fn validate(&self) -> Result<(), ConfigError>;
30}