Skip to main content

Crate confy_rs

Crate confy_rs 

Source
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 anywhere

Reloads 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

FeatureDefaultEffect
tomlyesTOML support via the toml crate
yamlnoYAML support via serde_yaml_ng
watchyeshot reloading via notify + arc-swap

JSON support is always built in.

Structs§

ConfigBuilder
A builder that loads configuration from multiple layered sources.
ConfigWatcher
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>.