Twelf
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 macrotwelf::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
Usage
Simple with JSON and environment variables
use ;
// Init configuration with layers, each layers override only existing fields
let config = with_layers.unwrap;
Example with clap support
use ;
// Will generate global arguments for each of your fields inside your configuration struct
let app = new.args;
// Init configuration with layers, each layers override only existing fields
let config = with_layers.unwrap;
// ... your application code
Check here for more examples.
TODO:
- Better error report with explicit layer name
- Suggest crates like https://github.com/jonasbb/serde_with and add usecases
- Support Vault
- Implement a trait/api to extend and let users fetch config from remote
- Refactor to let user extend layers
- Add support of nested struct in envy
- Fix issue with
#[serde(flatten)] when you use other type thanString` in sub types
Alternatives
- config-rs is almost doing the same except the environment layer (for example we support hashmap and array in environment variables). Also
config-rsdon't have clap support and it didn't use any proc-macros if you're not very fan of proc-macros.