Expand description
§More Configuration

More Configuration is a configuration library for Rust.
You may be looking for:
§Features
This crate provides the following features:
- default - Abstractions for configuration, including the std features
- std - Standard configuration implementation
- all - Includes all features, except async
- async - Use configuration in an asynchronous context
- mem - An in-memory configuration source
- env - An environment variables configuration source
- cmd - A command-line argument configuration source
- json - A *.json file configuration source
- xml - A *.xml file configuration source
- ini - An *.ini file configuration source
- chained - Chain multiple configuration sources
- binder - Bind a configuration to strongly-typed values and structs
Use
--features all,async
for all features with asynchronous support
§Configuration in Action
Consider the following demo.json
file:
{
"text": "Hello world!",
"demo": true,
"clients": [{
"region": "us-west",
"url": "https://tempuri.org"
}]
}
The configuration can be loaded, merged, and accessed from multiple sources:
use config::{*, ext::*};
fn main() {
let config = DefaultConfigurationBuilder::new()
.add_in_memory(&[("Demo", "false")])
.add_json_file("demo.json".is().optional())
.add_env_vars()
.add_command_line()
.build()
.unwrap();
if let Some(demo) = config.get("demo") {
if demo.as_str() == "true" {
println!("{}", config.get("Text").unwrap().as_str());
println!("{}", config.get("Clients:0:Region").unwrap().as_str());
return;
}
}
println!("Not a demo!");
}
Raw configuration values can be used, but they are much more interesting when we data bind them to strongly-typed values:
use serde::Deserialize;
#[derive(Default, Deserialize)]
#[serde(rename_all(deserialize = "PascalCase"))]
struct Client {
region: String,
url: String,
}
#[derive(Default, Deserialize)]
#[serde(rename_all(deserialize = "PascalCase"))]
struct AppOptions {
text: String,
demo: bool,
clients: Vec<Client>,
}
The first letter of JSON configuration keys are normalized to uppercase.
use config::{*, ext::*};
fn main() {
let file = std::env::current_exe()
.unwrap()
.parent()
.unwrap()
.join("../../demo.json");
let config = DefaultConfigurationBuilder::new()
.add_json_file(file)
.build()
.unwrap();
let app: AppOptions = config.reify();
if app.demo {
println!("{}", &app.text);
println!("{}", &app.clients[0].region);
return;
}
println!("Not a demo!");
}
§Minimum Supported Rust Version
When increasing the minimum supported Rust version (MSRV), the new version must have been released at least six months ago. The current MSRV is 1.60.
§License
This project is licensed under the MIT license.
Re-exports§
pub use util::*;
util
Modules§
Structs§
- Chained
Configuration Provider chained
- Represents a chained
ConfigurationProvider
. - Chained
Configuration Source chained
- Represents a chained
ConfigurationSource
. - Command
Line Configuration Provider cmd
- Represents a
ConfigurationProvider
that provides command line configuration values. - Command
Line Configuration Source cmd
- Represents a
ConfigurationSource
for command line data. - Configuration
Iterator - Represents an iterator of key/value pairs for a
Configuration
. - Default
Configuration Builder std
- Represents a configuration builder.
- Default
Configuration Root std
- Represents the root of a configuration.
- Default
Configuration Section std
- Represent a configuration section.
- Environment
Variables Configuration Provider env
- Represents a
ConfigurationProvider
for environment variables. - Environment
Variables Configuration Source env
- Represents a
ConfigurationSource
for environment variables. - File
Source - Represents a file configuration source.
- File
Source Builder - Represents a builder for a file source.
- IniConfiguration
Provider ini
- Represents a
ConfigurationProvider
for*.ini
files. - IniConfiguration
Source ini
- Represents a
ConfigurationSource
for*.ini
files. - Json
Configuration Provider json
- Represents a
ConfigurationProvider
for*.json
files. - Json
Configuration Source json
- Represents a
ConfigurationSource
for*.json
files. - Memory
Configuration Provider mem
- Represents a
ConfigurationProvider
that provides in-memory configuration values. - Memory
Configuration Source mem
- Represents a
ConfigurationSource
for in-memory data. - XmlConfiguration
Provider xml
- Represents a
ConfigurationProvider
for*.xml
files. - XmlConfiguration
Source xml
- Represents a
ConfigurationSource
for*.xml
files.
Enums§
- Configuration
Path - Represents a configuration path.
- Load
Error - Defines the possible load errors.
- Reload
Error - Defines the possible reload errors.
Traits§
- Configuration
- Defines the behavior of a configuration.
- Configuration
Builder - Defines the behavior used to build an application
Configuration
. - Configuration
Provider - Defines the behavior of an object that provides configuration key/values for an application.
- Configuration
Provider Iterator - Defines the behavior of an iterator over a
ConfigurationProvider
set. - Configuration
Root - Represents the root of a
Configuration
hierarchy. - Configuration
Section - Defines the behavior for a section of application configuration values.
- Configuration
Source - Represents a source of configuration key/value pairs for an application.
Type Aliases§
- Load
Result - Represents a configuration load result.
- Reload
Result - Represents a configuration reload result.
- Value
- Represents the type alias for a configuration value.