#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.

16 releases (6 breaking)

Uses new Rust 2021

0.7.0 Jul 25, 2022
0.5.0 May 15, 2022
0.3.1 Mar 25, 2022
0.1.7 Mar 15, 2021
0.1.2 Nov 11, 2020

#14 in Configuration

Download history 60/week @ 2022-04-22 166/week @ 2022-04-29 321/week @ 2022-05-06 364/week @ 2022-05-13 150/week @ 2022-05-20 131/week @ 2022-05-27 122/week @ 2022-06-03 49/week @ 2022-06-10 42/week @ 2022-06-17 70/week @ 2022-06-24 127/week @ 2022-07-01 45/week @ 2022-07-08 58/week @ 2022-07-15 140/week @ 2022-07-22 97/week @ 2022-07-29 90/week @ 2022-08-05

395 downloads per month
Used in 2 crates

MIT license

64KB
1K SLoC

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 (ATTENTION: if you're using version < v3 use the twelf@1.8 version)

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::Command::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.

Features

Twelf supports crate features, if you only want support for json, env and toml then you just have to add this to your Cargo.toml

twelf = { version = "0.3", default-features = false, features = ["json", "toml", "env"] }

Default features are ["env", "clap"]

Contributing

Feel free to contribute to the twelf project.

Enable all features when testing changes to the crate:

cargo test --all-features

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

~2.4–4MB
~86K SLoC