dotcfg
Flexible config management for Rust CLIs — your path, your format.
Why dotcfg?
Most config crates either lock you into a fixed directory strategy or only handle reading. dotcfg gives you full control:
- Choose
~/.toolname/or~/.config/toolname/— your call - TOML or JSON — feature-gated, include only what you need
- Load the whole config or get/set individual keys without touching the rest
- Returns
Noneif config doesn't exist — no magic, no forced defaults unless you want them
Installation
# TOML only (default)
= "0.1"
# JSON only
= { = "0.1", = false, = ["json"] }
# Both
= { = "0.1", = ["json"] }
Quick Start
use DotCfg;
use ;
Directory Strategy
// ~/.mytool/config.toml (default — like .cargo, .ssh)
let cfg = new;
// ~/.config/mytool/config.toml (XDG standard, cross-platform)
let cfg = new.xdg;
// explicit dot (same as default)
let cfg = new.dot;
Format
// TOML (default)
let cfg = new;
// JSON (needs `json` feature)
let cfg = new.json;
// Custom filename
let cfg = new.json.filename;
// → ~/.mytool/settings.json (or ~/.config/... with .xdg())
Loading
// Returns None if file doesn't exist — you decide what to do
let config: = cfg.load?;
// Returns an error if file doesn't exist — useful for CLIs that require setup
let config: Config = cfg.load_or_error?;
// Creates the file with Default values if it doesn't exist — opt-in
let config: Config = cfg.load_or_default?;
Per-key Get & Set
Get or set individual keys without loading or overwriting the whole config.
Supports flat and nested (one level: section.field) keys:
// Flat
cfg.get?;
cfg.set?;
// Nested → [user] table in TOML / nested object in JSON
cfg.get?;
cfg.set?;
set creates the file/dir if missing and preserves all other keys. Values are stored as strings.
Other Utilities
cfg.exists?; // does config file exist?
cfg.dir?; // config directory path
cfg.file_path?; // full file path
cfg.delete_file?; // delete file, keep dir
cfg.delete_dir?; // delete entire dir
Features
| Feature | Default | Description |
|---|---|---|
toml |
✅ | TOML format support |
json |
❌ | JSON format support |
License
Dual-licensed under MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.