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
impl ConfigBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty ConfigBuilder.
Examples found in repository?
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}Sourcepub fn add_source(self, content: &str, format: ConfigFormat) -> Result<Self>
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?
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}Sourcepub fn add_env_prefix(self, prefix: &str) -> Self
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?
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}Sourcepub fn build<T: DeserializeOwned>(self) -> Result<T>
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?
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
impl Debug for ConfigBuilder
Source§impl Default for ConfigBuilder
impl Default for ConfigBuilder
Source§fn default() -> ConfigBuilder
fn default() -> ConfigBuilder
Auto Trait Implementations§
impl Freeze for ConfigBuilder
impl RefUnwindSafe for ConfigBuilder
impl Send for ConfigBuilder
impl Sync for ConfigBuilder
impl Unpin for ConfigBuilder
impl UnsafeUnpin for ConfigBuilder
impl UnwindSafe for ConfigBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CodexOkWrap for T
impl<T> CodexOkWrap for T
Source§fn into_codex(self) -> CodexOk<T>
fn into_codex(self) -> CodexOk<T>
CodexOk, natively capturing the caller location.