Skip to main content

ConfigBuilder

Struct ConfigBuilder 

Source
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

Source

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));
Source

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 file
  • priority — lower numbers = higher precedence
Source

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 file
  • priority — lower numbers = higher precedence
Source

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 file
  • priority — lower numbers = higher precedence
Source

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 string
  • separator — delimiter in env var names that creates nesting (e.g. "__")
  • priority — lower numbers = higher precedence
Source

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 .env file
  • prefix — only capture env vars starting with this string
  • separator — delimiter in env var names that creates nesting
  • priority — lower numbers = higher precedence
Source

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.

Source

pub fn env_keys<I, S>( self, keys: I, separator: impl Into<String>, priority: u32, ) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

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.

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.

Source

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.

Trait Implementations§

Source§

impl Default for ConfigBuilder

Source§

fn default() -> ConfigBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more