pub struct Config { /* private fields */ }Expand description
A builder that collects Sources in priority order, then merges them.
Sources are applied in the order they are added; a later source overrides an earlier one for any leaf it sets. Unset leaves keep the earlier value, and a leaf no source sets falls back to the compiled-in defaults layer.
// `EXTRA` is added after `BASE`, so it wins for `host`.
let settings = Config::new()
.add_env("BASE")
.add_env("EXTRA")
.build::<SettingsProv>()
.unwrap();
assert_eq!(settings.host.value(), "from-extra");Implementations§
Source§impl Config
impl Config
Sourcepub fn add_source<S>(self, source: S) -> Config
pub fn add_source<S>(self, source: S) -> Config
Append any custom Source. The typed add_* helpers wrap the
built-in sources; reach for this to plug in your own.
Sourcepub fn add_toml_str(
self,
name: impl Into<String>,
content: impl Into<String>,
) -> Config
pub fn add_toml_str( self, name: impl Into<String>, content: impl Into<String>, ) -> Config
Add an in-memory TOML source. name is a human-readable label used in
error messages.
let settings = Config::new()
.add_toml_str("app.toml", "host = \"localhost\"\nport = 8080")
.build::<SettingsProv>()
.unwrap();
assert_eq!(settings.host.value(), "localhost");
assert_eq!(settings.host.source().category(), Category::File);Sourcepub fn add_env(self, prefix: impl AsRef<str>) -> Config
pub fn add_env(self, prefix: impl AsRef<str>) -> Config
Add an environment-variable source reading <PREFIX>_* variables.
Nested fields use __ as the separator. The variables are read now,
not when the config is built.
let settings = Config::new()
.add_env("MYAPP")
.build::<SettingsProv>()
.unwrap();
assert_eq!(settings.host.value(), "db.internal");
assert_eq!(settings.host.source().category(), Category::Env);Sourcepub fn add_env_with_list_keys<I, S>(
self,
prefix: impl AsRef<str>,
list_keys: I,
) -> Config
pub fn add_env_with_list_keys<I, S>( self, prefix: impl AsRef<str>, list_keys: I, ) -> Config
Add an environment-variable source where the given dotted paths are
treated as comma-separated lists. See sources::EnvSource::with_list_keys.
// `SVC_HOSTS` is a comma-separated list; name it so the source splits it.
let settings = Config::new()
.add_env_with_list_keys("SVC", ["hosts"])
.build::<SettingsProv>()
.unwrap();
assert_eq!(settings.hosts.value(), &["a.example", "b.example"]);Sourcepub fn add_cli<P>(self, partial: P) -> Config
pub fn add_cli<P>(self, partial: P) -> Config
Add a CLI source from a pre-built partial (every field an Option).
Typically produced from clap via the provcfg-clap ClapArgs derive.
let from_cli = SettingsPartial { host: Some("cli.example".to_string()), port: None };
let settings = Config::new()
.add_cli(from_cli)
.build::<SettingsProv>()
.unwrap();
assert_eq!(settings.host.value(), "cli.example");
assert_eq!(settings.host.source().category(), Category::Cli);
// `port` was `None` on the partial, so it falls back to the default.
assert_eq!(settings.port.source().category(), Category::Default);Sourcepub fn build<T>(&self) -> Result<T, Error>where
T: Provenance,
pub fn build<T>(&self) -> Result<T, Error>where
T: Provenance,
Merge every added source plus the implicit defaults layer into T, a
generated *Prov struct.
§Errors
Returns Error::Deserialize if a source’s data does not fit T.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Config
impl !UnwindSafe for Config
impl Forget for Config
impl Freeze for Config
impl Move for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnsafeUnpin for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more