Crates.io | more-config |
lib.rs | more-config |
version | 2.1.3 |
source | src |
created_at | 2022-10-06 19:29:25.454555 |
updated_at | 2024-06-28 20:35:51.051324 |
description | Provides support for configuration |
homepage | https://commonsensesoftware.github.io/more-rs-config/ |
repository | https://github.com/commonsensesoftware/more-rs-config |
max_upload_size | |
id | 681554 |
size | 119,965 |
More Configuration is a configuration library for Rust.
You may be looking for:
This crate provides the following features:
Use
--features all,async
for all features with asynchronous support
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!");
}
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.
This project is licensed under the MIT license.