Twelf is a configuration solution for Rust including 12-Factor support. It is designed with Layers in order to configure different sources and formats to build your configuration. The main goal is to be very simple using the proc macro twelf::config. For now it supports :
- Default settings (inside your codebase with
#[serde(default = ...)]coming from serde) - Reading from
TOML,YAML,JSON,DHALL,INIfiles - Reading from environment variables: it supports
HashMapstructure withMY_VARIABLE="mykey=myvalue,mykey2=myvalue2"and also array likeMY_VARIABLE=first,secondthanks to envy. - All serde attributes can be used in your struct to customize your configuration as you wish
- Reading your configuration from your command line built with clap
Examples
Simple with JSON and environment variables
use twelf::{config, Layer};
#[config]
struct Conf {
test: String,
another: usize,
}
// Init configuration with layers, each layers override only existing fields
let config = Conf::with_layers(&[
Layer::Json("conf.json".into()),
Layer::Env(Some("PREFIX_".to_string()))
]).unwrap();
Example with clap support
# use twelf::reexports::clap;
use twelf::{config, Layer};
#[config]
struct Conf {
/// Here is an example of documentation which is displayed in clap
test: String,
another: usize,
}
// Will generate global arguments for each of your fields inside your configuration struct
let app = clap::App::new("test").args(&Conf::clap_args());
// Init configuration with layers, each layers override only existing fields
let config = Conf::with_layers(&[
Layer::Json("conf.json".into()),
Layer::Env(Some("PREFIX_".to_string())),
Layer::Clap(app.get_matches().clone())
]).unwrap();
// ... your application code