tomldir
tomldir is a lean crate for loading TOML configs into map-based structures with fast dot-path access, deterministic flattening, and easy composition - ideal for CLIs and runtime-driven configuration.
Features
- Runtime-friendly: Load and query config via string key paths like
"server.port". - Flattened output: Nested tables become dot-separated keys for easy iteration or overlay with env/flags.
- Map-first storage: Uses a
HashMapby default, but easily swap inIndexMaporBTreeMap. - Thread-safe: Designed for use with
Arcand concurrent access. - Flexible flattening: Flatten to any supported map or container with
flatten_into().
If you’re coming from Go, tomldir offers a Viper-like workflow: load TOML into a map, access values via dot paths, and flatten configs for runtime use.
Why tomldir?
Use tomldir when you want:
- Runtime access via string paths like
database.host - Easy flattening for CLI flags or environment overlays
- Flexible configs without rigid structs
tomldir intentionally prioritizes simplicity over strong typing.
Usage
Add to Cargo.toml:
[]
= "0.1"
# Optional: for order-preserving keys
= "2.0"
Basic: Setting up a runtime config
use Config;
Advanced: Preserving Order with IndexMap
If you need to iterate over keys in the order they were defined (e.g., for help text generation or consistent output), use IndexMap.
use ;
use IndexMap;
You can get an iterator over all flattened key-value pairs as strings:
for in cfg.flatten
Or collect them into a specific map (like HashMap<String, String>) using flatten_into():
let flat: = cfg.flatten_into;
Or flatten into any custom type using flatten_into():
use BTreeMap;
// Flatten to BTreeMap (sorted keys)
let flat_sorted: = cfg.flatten_into;
// Flatten to Vec
let flat_vec: = cfg.flatten_into;
Comparison
| Feature | serde + toml | config | tomldir |
|---|---|---|---|
| Strong typing | ✅ | ⚠️ | ❌ |
| Dot-path runtime access | ❌ | ⚠️ | ✅ |
| Flattening support | ❌ | ⚠️ | ✅ |
| Minimal boilerplate | ❌ | ⚠️ | ✅ |
License
MIT