Skip to main content

ConfigLoaderBuilder

Struct ConfigLoaderBuilder 

Source
pub struct ConfigLoaderBuilder { /* private fields */ }
Expand description

Builder for loading configuration from TOML files and environment variables.

§Example

use fabryk_cli::config_loader::ConfigLoaderBuilder;
use serde::Deserialize;

#[derive(Default, Deserialize)]
struct MyConfig {
    port: u16,
}

let resolve = |explicit: Option<&str>| -> Option<std::path::PathBuf> {
    explicit.map(std::path::PathBuf::from)
};

let (config, path) = ConfigLoaderBuilder::new("myapp")
    .section("server")
    .section("logging")
    .port_env_override("PORT")
    .build::<MyConfig>(None, resolve)
    .unwrap();

Implementations§

Source§

impl ConfigLoaderBuilder

Source

pub fn new(prefix: &str) -> Self

Create a new builder with the given environment variable prefix.

The prefix is used for env var namespacing (e.g., "taproot"TAPROOT_* environment variables).

Source

pub fn section(self, name: &str) -> Self

Register a config section for environment variable mapping.

Each section becomes a namespace in the env var hierarchy. For example, section "bq" maps TAPROOT_BQ_PROJECTbq.project.

Source

pub fn port_env_override(self, env_var: &str) -> Self

Register a bare environment variable that overrides the port field.

Cloud Run sets PORT (without prefix). This option lets the loader check that env var and apply it as a post-load override.

The override only applies if the env var exists and parses as u16. The caller is responsible for applying the override to the correct field after build() returns.

Source

pub fn build<C: DeserializeOwned + Default>( self, config_path: Option<&str>, resolve_fn: impl Fn(Option<&str>) -> Option<PathBuf>, ) -> Result<(C, Option<PathBuf>)>

Build the configuration, returning the deserialized struct and the resolved config file path (if one was found).

§Arguments
  • config_path — explicit config file path (e.g., from --config flag)
  • resolve_fn — function to resolve the config path when not explicit
§Errors

Returns an error if the config file exists but cannot be parsed, or if environment variables contain invalid values.

Source

pub fn check_port_override(&self) -> Option<u16>

Check the port environment variable override, if configured.

Returns Some(port) if the env var exists and parses as u16. Intended to be called after build() to apply the override.

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, 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