A library for managing application configurations with support for multiple file formats.
prefer helps you manage application configurations while providing users the flexibility of using whatever configuration format fits their needs. It automatically discovers configuration files in standard system locations and supports JSON, JSON5, YAML, TOML, INI, and XML formats.
- Format-agnostic: Supports JSON, JSON5, YAML, TOML, INI, and XML
- Automatic discovery: Searches standard system paths for configuration files
- Async by design: Non-blocking operations for file I/O
- File watching: Monitor configuration files for changes
- Dot-notation access: Access nested values with simple key strings
- Cross-platform: Works on Linux, macOS, and Windows
Add this to your Cargo.toml:
[dependencies]
prefer = "0.1"use prefer::load;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = load("settings").await?;
let username: String = config.get("auth.username").await?;
let port: u16 = config.get("server.port").await?;
println!("Username: {}", username);
println!("Port: {}", port);
Ok(())
}use prefer::watch;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut receiver = watch("settings").await?;
while let Some(config) = receiver.recv().await {
let port: u16 = config.get("server.port").await?;
println!("Configuration updated! Port: {}", port);
}
Ok(())
}The library automatically detects and parses the following formats:
- JSON (
.json) - JSON5 (
.json5,.jsonc) - with comments and trailing commas - YAML (
.yaml,.yml) - TOML (
.toml) - INI (
.ini) - XML (
.xml)
prefer searches for configuration files in the following locations (in order):
- Current directory
$XDG_CONFIG_HOME(or~/.config)$XDG_CONFIG_DIRS$HOME/usr/local/etc/usr/etc/etc
- Current directory
%USERPROFILE%%APPDATA%%ProgramData%%SystemRoot%
By default, all format parsers are enabled. You can disable specific formats:
[dependencies]
prefer = { version = "0.1", default-features = false, features = ["json5"] }Available features:
json5- JSON5 format supportxml- XML format supportini- INI format support
Note: JSON, YAML, and TOML are always available.
See the examples directory for more usage examples:
basic.rs- Basic configuration loadingwatch.rs- File watching for live updates
Run examples with:
cargo run --example basic
cargo run --example watchMIT