confy-rs
English | 简体中文
Simple, efficient configuration loading for Rust — multi-format, layered merging, and hot reloading.
Features
- Multi-format — TOML / YAML / JSON, auto-detected from the file extension
- Layered — in-memory defaults plus any number of files, deep-merged in order
- Type-safe — deserializes straight into your own structs via serde
- Hot reload — debounced cross-platform file watching; readers are lock-free, and a failed reload keeps the previous configuration active
- Lean — a handful of small dependencies, everything non-essential is feature-gated, no async runtime required
Installation
[]
= "0.1"
TOML and hot reloading are enabled by default. YAML is opt-in:
= { = "0.1", = ["yaml"] }
Quick start
use Deserialize;
Layered configuration
Sources are merged in the order they are added — later sources override
earlier ones. Objects merge recursively; scalars, arrays and null replace
the previous value entirely.
let config: AppConfig = builder
.defaults? // lowest priority
.file // required, error if missing
.file_optional // silently skipped if missing
.build?;
Hot reloading
let watcher = builder
.file
.?;
watcher.on_change;
watcher.on_error;
let current = watcher.get; // Arc<AppConfig>, lock-free
What you get:
- Fail fast — the initial load is synchronous; a broken config errors at startup, not later
- Editor-friendly — parent directories are watched, so atomic-rename saves (vim, VS Code, ...) are handled
- Debounced — bursts of file events collapse into a single reload; unchanged content triggers no callbacks
- Resilient — a failed reload never crashes or corrupts state: the previous config stays active and the error is reported via
on_error - Fast reads —
watcher.get()is lock-free (arc-swap), cheap enough for per-request use - Runtime-agnostic — one background thread, no tokio required; call
get()from async code freely
Cargo features
| Feature | Default | Effect |
|---|---|---|
toml |
✅ | TOML support via the toml crate |
yaml |
— | YAML support via serde_yaml_ng |
watch |
✅ | hot reloading via notify + arc-swap |
JSON support is always built in.
Examples
MSRV
Rust 1.96.0 (pinned by rust-toolchain.toml).
Development
&&
Common commands:
See CONTRIBUTING.md for the full workflow.
License
Licensed under MIT.