Expand description
Simple, efficient configuration loading for Rust.
confy-rs loads configuration from multiple file formats, merges layered
sources and deserializes the result into your own types via serde. With
the watch feature it also hot-reloads the configuration when source
files change on disk.
§Highlights
- Multi-format: TOML / YAML / JSON, detected from the file extension
- Layered: defaults + any number of files, deep-merged in order
- Hot reload: debounced cross-platform file watching; readers are lock-free and the previous config is kept when a reload fails
- Lean: format parsers and the watcher are feature-gated
§Quick start
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct AppConfig {
name: String,
port: u16,
}
let config: AppConfig = confy_rs::load_str(
r#"
name = "demo"
port = 8080
"#,
confy_rs::Format::Toml,
)?;
assert_eq!(config.port, 8080);Loading from files works the same way — the format is detected from the extension, and later sources override earlier ones:
// One-liner for the common case
let config: AppConfig = confy_rs::load("config.toml")?;
// Layered loading
let config: AppConfig = confy_rs::builder()
.file("config/default.toml")
.file_optional("config/local.json")
.build()?;§Hot reloading
let watcher = confy_rs::builder()
.file("config.toml")
.watch::<AppConfig>()?;
watcher.on_change(|config| println!("port is now {}", config.port));
let current = watcher.get(); // lock-free snapshot, callable anywhereReloads are debounced, editors that save via atomic rename are handled,
and when a reload fails the previous configuration stays active while the
error is reported through ConfigWatcher::on_error.
§Cargo features
| Feature | Default | Effect |
|---|---|---|
toml | yes | TOML support via the toml crate |
yaml | no | YAML support via serde_yaml_ng |
watch | yes | hot reloading via notify + arc-swap |
JSON support is always built in.
Structs§
- Config
Builder - A builder that loads configuration from multiple layered sources.
- Config
Watcher - A handle to a configuration that is automatically reloaded when any of its source files change.
Enums§
- Error
- All errors that can occur while loading or watching configuration.
- Format
- Supported configuration file formats.
Functions§
- builder
- Creates a new empty
ConfigBuilder. - load
- Loads a single configuration file and deserializes it into
T. - load_
str - Parses configuration from an in-memory string in the given format.
Type Aliases§
- Result
- Convenience alias for
std::result::Result<T, confy_rs::Error>.