MCPcopy Index your code
hub / github.com/bnjjj/twelf

github.com/bnjjj/twelf @0.1.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.1.2 ↗ · + Follow
69 symbols 90 edges 15 files 1 documented · 1%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

Use features to improve compile time

If you don't want to include useless crates if you just use 2 of all available layers you can use features without default-features, example if you use only yaml and env layer.

[dependencies]
twelf = { version = "0.1", default-features = false, features = ["yaml"] }

Check here for more examples.

TODO:

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

Core symbols most depended-on inside this repo

parse
called by 0
config-derive/src/attr.rs
config
called by 0
config-derive/src/lib.rs
main
called by 0
twelf/examples/advanced_struct.rs
get_default_value
called by 0
twelf/examples/default_env.rs
main
called by 0
twelf/examples/default_env.rs
main
called by 0
twelf/examples/simple_env_json.rs
main
called by 0
twelf/examples/clap.rs

Shape

Function 46
Class 20
Enum 2
Method 1

Languages

Rust100%

Modules by API surface

twelf/tests/mixed.rs12 symbols
twelf/tests/yaml.rs8 symbols
twelf/tests/toml.rs8 symbols
twelf/tests/json.rs8 symbols
twelf/tests/ini.rs8 symbols
twelf/tests/dhall.rs8 symbols
twelf/examples/default_env.rs3 symbols
twelf/examples/advanced_struct.rs3 symbols
twelf/tests/clap.rs2 symbols
twelf/examples/simple_env_json.rs2 symbols
twelf/examples/clap.rs2 symbols
config-derive/src/attr.rs2 symbols

For agents

$ claude mcp add twelf \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact