pub struct Loader<T> { /* private fields */ }Expand description
Merges configuration sources and converts the result into T.
Sources are merged in ascending priority order; a higher priority value overrides a lower one, and equal priorities keep insertion order, with the later source winning.
| Source | Priority |
|---|---|
declared or added files (ConfigFile) | 10 |
source default | 100 |
| environment variables, then dotenv | 200 |
| command line | 250 |
A null value never overrides a lower source, arrays replace instead of appending, and tables merge key by key.
Implementations§
Source§impl<T: FromNode> Loader<T>
impl<T: FromNode> Loader<T>
Sourcepub fn file(self, file: ConfigFile) -> Self
pub fn file(self, file: ConfigFile) -> Self
Add a configuration file.
Sourcepub fn source(self, source: impl ConfigSource + 'static) -> Self
pub fn source(self, source: impl ConfigSource + 'static) -> Self
Add any ConfigSource; its priority
decides the merge order.
Sourcepub fn env_vars<I, K, V>(self, vars: I) -> Self
pub fn env_vars<I, K, V>(self, vars: I) -> Self
Use vars instead of the process environment. Meant for tests, which
then never touch global state.
Sourcepub fn on_warning(
self,
handler: impl Fn(&Warning) + Send + Sync + 'static,
) -> Self
pub fn on_warning( self, handler: impl Fn(&Warning) + Send + Sync + 'static, ) -> Self
Replace the default warning handler (which prints warning: ... to
stderr). Called once per deprecated key present in the merged tree.
Sourcepub fn load(&self) -> Result<T, ConfigError>
pub fn load(&self) -> Result<T, ConfigError>
Load every source, merge them, report deprecations and convert.
§Errors
The first source error, or the conversion errors of T (all of them
at once when several fields fail).
Source§impl<T: FromNode> Loader<T>
impl<T: FromNode> Loader<T>
Sourcepub fn cli(self) -> Self
pub fn cli(self) -> Self
Read flags from the process arguments (std::env::args_os).
Flags are derived from T: --port, --server.max-conn, -p with
short, --verbose / --no-verbose for booleans. Repeated list flags
append, -h / --help yields a Help error
from load. See load_or_exit.
Sourcepub fn cli_args<I>(self, args: I) -> Self
pub fn cli_args<I>(self, args: I) -> Self
Like cli with explicit arguments. args is the full
argv, so the first item is the program name. Meant for tests.
Sourcepub fn load_or_exit(&self) -> T
pub fn load_or_exit(&self) -> T
load, then exit the process on failure: --help
prints the help text to stdout and exits with status 0, any other
error prints error: ... to stderr and exits with status 2.