Crates.io | realme |
lib.rs | realme |
version | 0.2.3 |
created_at | 2024-09-16 12:09:35.680967+00 |
updated_at | 2025-07-28 07:29:38.433289+00 |
description | A flexible and extensible configuration management library for Rust, designed to simplify the process of loading and managing configuration settings from various sources. |
homepage | https://github.com/vainjoker/realme |
repository | https://github.com/vainjoker/realme |
max_upload_size | |
id | 1376270 |
size | 337,234 |
Realme is a flexible and extensible configuration management library for Rust. It greatly simplifies the process of loading and managing configurations from multiple data sources by decoupling the configuration Source from the Parser.
Realme's design revolves around several core components:
Source
: Defines the origin of configuration data. It can be a file (FileSource
), environment variables (EnvSource
), command-line arguments (CmdSource
), a string (StringSource
), or a serialized object (SerSource
).Parser
: Defines how to parse raw data into configuration values. For example, TomlParser
is used for parsing TOML format, JsonParser
for JSON format, and SerParser
for serialized objects.Adaptor
: An adapter that connects a Source
and a Parser
together, telling Realme
where to read the data from and how to parse it.Realme
: The core configuration object that loads multiple Adaptors
in sequence, merging the parsed configuration data into a unified view. Later configurations will override earlier ones with the same name.Source
and Parser
traitsplaceholder
feature) Supports using Tera template syntax in configuration valuesAdd Realme to your Cargo.toml
:
cargo add realme
Then enable the required features in your Cargo.toml
. For example, to use TOML and environment variables:
[dependencies]
realme = { version = "0.2.2", features = ["toml", "env"] }
serde = { version = "1", features = ["derive"] }
By default, Realme enables env
and macros
features. To use all features, you can enable the full
feature:
[dependencies]
realme = { version = "0.2.2", features = ["full"] }
Here's a simple example of reading a TOML configuration string and deserializing it into a struct:
use realme::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Settings {
name: String,
age: u32,
features: Vec<String>,
}
const CONFIG_DATA: &str = r#"
name = "MyApp"
age = 42
features = ["fast", "reliable"]
"#;
fn main() -> Result<(), realme::Error> {
let realme = Realme::builder()
.load(Adaptor::new(StringSource::<TomlParser>::new(CONFIG_DATA)))
.build()?;
// Deserialize into struct
let settings: Settings = realme.try_deserialize()?;
println!("{:#?}", settings);
// Or get individual values
let name: String = realme.get_as("name")?.unwrap();
println!("App name: {}", name);
Ok(())
}
A common pattern is to load configuration from multiple sources:
use realme::prelude::*;
fn main() -> Result<(), realme::Error> {
let realme = Realme::builder()
// 1. Load default configuration
.load(Adaptor::new(FileSource::<TomlParser>::new("config/default.toml")))
// 2. Load environment-specific configuration
.load(Adaptor::new(FileSource::<TomlParser>::new("config/dev.toml"))
.profile("dev"))
// 3. Override with environment variables
.load(Adaptor::new(EnvSource::<EnvParser>::new("APP", Some("_"))))
.profile("dev") // Select dev environment
.build()?;
println!("Final config: {:#?}", realme);
Ok(())
}
Feature | Description | Dependencies |
---|---|---|
full |
Enables all features below | - |
env |
Default enabled, parses environment variable config | - |
macros |
Default enabled, provides procedural macros | realme_macros |
placeholder |
Enables tera -based placeholder substitution |
tera |
watch |
Enables file hot-reloading functionality | notify , crossbeam |
tracing |
Integrates with tracing library for logging |
tracing |
cmd |
Parses configuration from command-line arguments | clap , nom |
toml |
Adds TOML format support | toml |
json |
Adds JSON format support | serde_json |
yaml |
Adds YAML format support | serde_yaml2 |
json5 |
Adds JSON5 format support | serde_json5 |
ron |
Adds Rusty Object Notation (RON) support | ron |
ini |
Adds INI format support | rust-ini |
I drew inspiration from the following excellent libraries:
Compared to them, Realme's distinguishing features are:
serde
Conversion: All value conversions are based on serde
, enabling seamless type conversionSource
and Parser
traits, easily extend support for new data sources and formatsAll forms of contributions are welcome! Please feel free to submit a Pull Request.
This project is dual-licensed under both Apache License, Version 2.0 and MIT license.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.