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
impl ConfigLoaderBuilder
Sourcepub fn new(prefix: &str) -> Self
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).
Sourcepub fn section(self, name: &str) -> Self
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_PROJECT → bq.project.
Sourcepub fn port_env_override(self, env_var: &str) -> Self
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.
Sourcepub fn build<C: DeserializeOwned + Default>(
self,
config_path: Option<&str>,
resolve_fn: impl Fn(Option<&str>) -> Option<PathBuf>,
) -> Result<(C, Option<PathBuf>)>
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--configflag)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.
Sourcepub fn check_port_override(&self) -> Option<u16>
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.