Crates.io | layeredconf |
lib.rs | layeredconf |
version | 0.2.3 |
source | src |
created_at | 2021-12-05 03:11:37.212339 |
updated_at | 2022-01-22 22:13:33.726729 |
description | Layered configeration files, deserialized with serde |
homepage | |
repository | https://github.com/GothAck/layeredconf |
max_upload_size | |
id | 492513 |
size | 50,180 |
Yet Another Config Package
Hopefully this one will be useful to someone. Incoming features:
Add layeredconf
, clap
, and serde
to your Cargo.toml
[dependencies]
layeredconf = "0.2.0"
clap = "3.0.0-beta.5"
serde = { version = "1.0", features = ["derive"] }
Define your config
use layeredconf::{Builder, Format, LayeredConf, Source};
use serde::Deserialize;
#[derive(LayeredConf, Deserialize)]
struct Config {
/// Will also load this config file
#[layered(load_config)]
#[clap(long)]
config: Option<std::path::PathBuf>,
/// Required to be set in at least one Layer (config file, command line, etc.)
#[clap(long)]
name: String,
/// Optional field
#[clap(long)]
input: Option<String>,
/// Defaulted field
#[layered(default)]
#[clap(long)]
number: u32,
}
fn main() -> anyhow::Result<()> {
let config: Config = Builder)::new()
.new_layer(Source::OptionalFile {
path: "/etc/my_app/config.yaml",
format: Format::Auto,
})
.new_layer(Source::File {
path: "relative/config.yaml",
format: Format::Auto,
})
.new_layer(Source::Arguments)
.solidify()?;
// Use config in your application
}