| Crates.io | prefer |
| lib.rs | prefer |
| version | 0.3.4 |
| created_at | 2025-11-26 22:33:20.541994+00 |
| updated_at | 2026-01-24 07:44:56.086659+00 |
| description | A lightweight library for managing application configurations with support for multiple file formats |
| homepage | |
| repository | https://github.com/LimpidTech/prefer.rs |
| max_upload_size | |
| id | 1952576 |
| size | 215,600 |
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.
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).json5, .jsonc) - with comments and trailing commas.yaml, .yml).toml).ini).xml)prefer searches for configuration files in the following locations (in order):
$XDG_CONFIG_HOME (or ~/.config)$XDG_CONFIG_DIRS$HOME/usr/local/etc/usr/etc/etc%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 supportNote: 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 updatesRun examples with:
cargo run --example basic
cargo run --example watch
MIT