pub struct ConfigBuilder { /* private fields */ }Expand description
Fluent builder for assembling configuration sources in priority order.
Sources are loaded in priority order: lower priority numbers = higher precedence. When multiple sources define the same key, the source with the lowest priority wins.
§Example
use stratify::ConfigBuilder;
let store = ConfigBuilder::default()
.json("config/base.json", 100)
.yaml("config/override.yaml", 50)
.env("APP_", "__", 10)
.build().await
.unwrap();Implementations§
Source§impl ConfigBuilder
impl ConfigBuilder
Sourcepub fn source(self, source: impl Source + 'static) -> Self
pub fn source(self, source: impl Source + 'static) -> Self
Add an arbitrary Source implementation.
Use this for custom sources that aren’t covered by the convenience methods
(.json(), .yaml(), .env(), .dotenv()).
§Example
use stratify::ConfigBuilder;
use stratify::source::JsonSource;
let builder = ConfigBuilder::default()
.source(JsonSource::new("config/app.json", 100));Sourcepub fn json(self, path: impl AsRef<Path>, priority: u32) -> Self
pub fn json(self, path: impl AsRef<Path>, priority: u32) -> Self
Add a JSON file source.
Loads a .json file and merges its contents at the given priority.
§Parameters
path— path to the JSON filepriority— lower numbers = higher precedence
Sourcepub fn yaml(self, path: impl AsRef<Path>, priority: u32) -> Self
pub fn yaml(self, path: impl AsRef<Path>, priority: u32) -> Self
Add a YAML file source.
Loads a .yaml or .yml file and merges its contents at the given priority.
§Parameters
path— path to the YAML filepriority— lower numbers = higher precedence
Sourcepub fn toml(self, path: impl AsRef<Path>, priority: u32) -> Self
pub fn toml(self, path: impl AsRef<Path>, priority: u32) -> Self
Add a TOML file source.
Loads a .toml file, converts it to JSON internally, and merges its
contents at the given priority.
§Parameters
path— path to the TOML filepriority— lower numbers = higher precedence
Sourcepub fn env(
self,
prefix: impl Into<String>,
separator: impl Into<String>,
priority: u32,
) -> Self
pub fn env( self, prefix: impl Into<String>, separator: impl Into<String>, priority: u32, ) -> Self
Add an environment variable source.
Captures all env vars matching prefix, strips the prefix, lowercases the key,
and converts separator to . for nesting.
§Example
With prefix "APP_" and separator "__":
APP_HOST=localhost→{"host": "localhost"}APP_DB__PORT=5432→{"db": {"port": "5432"}}
§Parameters
prefix— only capture env vars starting with this stringseparator— delimiter in env var names that creates nesting (e.g."__")priority— lower numbers = higher precedence
Sourcepub fn dotenv(
self,
path: impl AsRef<Path>,
prefix: impl Into<String>,
separator: impl Into<String>,
priority: u32,
) -> Result<Self, ConfigError>
pub fn dotenv( self, path: impl AsRef<Path>, prefix: impl Into<String>, separator: impl Into<String>, priority: u32, ) -> Result<Self, ConfigError>
Add a .env file source.
Loads environment variables from a .env file via dotenvy,
then captures them using the same prefix/separator semantics as .env().
§Errors
Returns Err if the file cannot be read or parsed.
§Parameters
path— path to the.envfileprefix— only capture env vars starting with this stringseparator— delimiter in env var names that creates nestingpriority— lower numbers = higher precedence
Sourcepub fn azure(
self,
endpoint: impl Into<String>,
credential: Arc<dyn TokenCredential>,
priority: u32,
) -> Self
pub fn azure( self, endpoint: impl Into<String>, credential: Arc<dyn TokenCredential>, priority: u32, ) -> Self
Return all registered sources, sorted by priority (ascending).
Lower priority numbers come first — they have higher precedence during merging. Add an Azure App Configuration source.
Available under the azure feature. The credential is supplied by the
caller so that a service can use a managed identity in Azure and a
developer credential locally without this crate choosing for it.
For a label filter or a non-default separator, construct
AzureAppConfigSource directly
and pass it to ConfigBuilder::source.
priority follows the crate convention: lower numbers win.
Sourcepub fn env_keys<I, S>(
self,
keys: I,
separator: impl Into<String>,
priority: u32,
) -> Self
pub fn env_keys<I, S>( self, keys: I, separator: impl Into<String>, priority: u32, ) -> Self
Add a source over exactly the named environment variables.
For settings named by convention rather than by application, such as
RUST_LOG or AZURE_STORAGE_ACCOUNT, where no prefix selects them and
nothing else. See EnvSource::with_keys.
priority follows the crate convention: lower numbers win.
Sourcepub fn build_sources(self) -> Vec<Arc<dyn Source>>
pub fn build_sources(self) -> Vec<Arc<dyn Source>>
Consume the builder and return its sources, ordered by precedence.
Sorted so that the lowest priority number comes first. Most callers want
ConfigBuilder::build instead; this is exposed for anyone assembling a
ConfigStore by hand.
Sourcepub async fn build(self) -> Result<ConfigStore, ConfigError>
pub async fn build(self) -> Result<ConfigStore, ConfigError>
Build and load all sources into a ConfigStore.
This is the terminal operation — after calling build(), you get a
fully-loaded, cached ConfigStore ready for querying.
§Errors
Returns Err if any source fails to load.