#config #configuration #env #environment #settings

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 a proc macro.

8 releases

0.1.7 Mar 15, 2021
0.1.6 Jan 25, 2021
0.1.2 Nov 11, 2020

#26 in Configuration

Download history 1/week @ 2020-12-19 12/week @ 2021-01-02 47/week @ 2021-01-09 26/week @ 2021-01-16 72/week @ 2021-01-23 70/week @ 2021-01-30 53/week @ 2021-02-06 126/week @ 2021-02-13 40/week @ 2021-02-20 76/week @ 2021-02-27 80/week @ 2021-03-06 123/week @ 2021-03-13 115/week @ 2021-03-20 128/week @ 2021-03-27 113/week @ 2021-04-03

205 downloads per month

MIT and maybe AGPL-3.0

19KB
68 lines

Twelf

Rust Rust Version Docs.rs

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, INI files
  • Reading from environment variables: it supports HashMap structure with MY_VARIABLE="mykey=myvalue,mykey2=myvalue2" and also array like MY_VARIABLE=first,second thanks 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 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::{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

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 than String` 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-rs don't have clap support and it didn't use any proc-macros if you're not very fan of proc-macros.

Dependencies

~8MB
~180K SLoC