Skip to main content

ConfigBuilder

Struct ConfigBuilder 

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

A robust builder for constructing, merging, and resolving configurations.

ConfigBuilder aggregates settings from multiple discrete sources (such as structured files and environment variables), seamlessly merging them into a unified internal representation before deserializing into a strongly-typed structure.

Implementations§

Source§

impl ConfigBuilder

Source

pub fn new() -> Self

Creates a new, empty ConfigBuilder.

Examples found in repository?
examples/basic_usage.rs (line 24)
12fn main() {
13  let ron_content = r#"
14        (
15            app_name: "My Awesome App",
16            debug_mode: true,
17        )
18    "#;
19
20  std::env::set_var("APP_DEBUG_MODE", "false");
21
22  println!("Loading configuration...");
23
24  let result = cirious_codex_config::ConfigBuilder::new()
25    .add_source(ron_content, ConfigFormat::Ron)
26    .unwrap()
27    .value
28    .add_env_prefix("APP_")
29    .build::<AppSettings>();
30
31  match result {
32    Ok(ok_wrapper) => {
33      let settings = ok_wrapper.value;
34      println!("App Name: {}", settings.app_name);
35      println!("Debug Mode: {}", settings.debug_mode);
36    }
37    Err(e) => eprintln!("Failed: {e}"),
38  }
39}
Source

pub fn add_source(self, content: &str, format: ConfigFormat) -> Result<Self>

Incorporates a structured configuration source into the builder.

This method parses the provided raw string content according to the specified ConfigFormat and recursively merges the resulting data into the current configuration state.

§Errors

Returns an error if the content cannot be parsed by the specified format.

Examples found in repository?
examples/basic_usage.rs (line 25)
12fn main() {
13  let ron_content = r#"
14        (
15            app_name: "My Awesome App",
16            debug_mode: true,
17        )
18    "#;
19
20  std::env::set_var("APP_DEBUG_MODE", "false");
21
22  println!("Loading configuration...");
23
24  let result = cirious_codex_config::ConfigBuilder::new()
25    .add_source(ron_content, ConfigFormat::Ron)
26    .unwrap()
27    .value
28    .add_env_prefix("APP_")
29    .build::<AppSettings>();
30
31  match result {
32    Ok(ok_wrapper) => {
33      let settings = ok_wrapper.value;
34      println!("App Name: {}", settings.app_name);
35      println!("Debug Mode: {}", settings.debug_mode);
36    }
37    Err(e) => eprintln!("Failed: {e}"),
38  }
39}
Source

pub fn add_env_prefix(self, prefix: &str) -> Self

Incorporates environment variables into the configuration state.

This method iterates through the current process’s environment variables, filtering for those that start with the provided prefix. Matching variables have their prefix stripped and are converted to lowercase to align with standard configuration property casing.

Examples found in repository?
examples/basic_usage.rs (line 28)
12fn main() {
13  let ron_content = r#"
14        (
15            app_name: "My Awesome App",
16            debug_mode: true,
17        )
18    "#;
19
20  std::env::set_var("APP_DEBUG_MODE", "false");
21
22  println!("Loading configuration...");
23
24  let result = cirious_codex_config::ConfigBuilder::new()
25    .add_source(ron_content, ConfigFormat::Ron)
26    .unwrap()
27    .value
28    .add_env_prefix("APP_")
29    .build::<AppSettings>();
30
31  match result {
32    Ok(ok_wrapper) => {
33      let settings = ok_wrapper.value;
34      println!("App Name: {}", settings.app_name);
35      println!("Debug Mode: {}", settings.debug_mode);
36    }
37    Err(e) => eprintln!("Failed: {e}"),
38  }
39}
Source

pub fn build<T: DeserializeOwned>(self) -> Result<T>

Finalizes the build process and deserializes the aggregated configuration.

This method consumes the ConfigBuilder, attempting to map the deeply merged internal JSON representation into the designated strongly-typed structure T.

§Errors

Returns an error if the merged configuration cannot be mapped to the target type T.

Examples found in repository?
examples/basic_usage.rs (line 29)
12fn main() {
13  let ron_content = r#"
14        (
15            app_name: "My Awesome App",
16            debug_mode: true,
17        )
18    "#;
19
20  std::env::set_var("APP_DEBUG_MODE", "false");
21
22  println!("Loading configuration...");
23
24  let result = cirious_codex_config::ConfigBuilder::new()
25    .add_source(ron_content, ConfigFormat::Ron)
26    .unwrap()
27    .value
28    .add_env_prefix("APP_")
29    .build::<AppSettings>();
30
31  match result {
32    Ok(ok_wrapper) => {
33      let settings = ok_wrapper.value;
34      println!("App Name: {}", settings.app_name);
35      println!("Debug Mode: {}", settings.debug_mode);
36    }
37    Err(e) => eprintln!("Failed: {e}"),
38  }
39}

Trait Implementations§

Source§

impl Debug for ConfigBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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> CodexOkWrap for T

Source§

fn into_codex(self) -> CodexOk<T>

Wraps the current value into a CodexOk, natively capturing the caller location.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.