Realme
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.
Core Concepts
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,TomlParseris used for parsing TOML format,JsonParserfor JSON format, andSerParserfor serialized objects.Adaptor: An adapter that connects aSourceand aParsertogether, tellingRealmewhere to read the data from and how to parse it.Realme: The core configuration object that loads multipleAdaptorsin sequence, merging the parsed configuration data into a unified view. Later configurations will override earlier ones with the same name.
Key Features
- Layered Configuration: Load configurations from multiple sources in order, such as: default configuration file → environment-specific file → environment variables → command-line arguments
- Multi-Source Support: Built-in support for files, environment variables, command-line arguments, strings, and serialized objects as configuration sources
- Multi-Format Parsing: Supports popular formats like TOML, JSON, YAML, JSON5, RON, and INI through feature flags
- Profile Support: Supports multi-environment configuration, allowing different settings for different environments (e.g., dev, prod, test)
- Hot Reload: Can monitor configuration file changes and automatically reload the configuration at runtime without restarting the application
- Strong and Weak Typing: Configuration values can be deserialized into strongly-typed Rust structs, and also accessed as weakly-typed values at runtime
- Fully Extensible: You can easily add custom data sources and parsers by implementing the
SourceandParsertraits - Placeholder/Template Support: (Via the
placeholderfeature) Supports using Tera template syntax in configuration values - Macro Support: Provides convenient macros to simplify the configuration building process
Installation
Add Realme to your Cargo.toml:
Then enable the required features in your Cargo.toml. For example, to use TOML and environment variables:
[]
= { = "0.2.2", = ["toml", "env"] }
= { = "1", = ["derive"] }
By default, Realme enables env and macros features. To use all features, you can enable the full feature:
[]
= { = "0.2.2", = ["full"] }
Quick Start
Here's a simple example of reading a TOML configuration string and deserializing it into a struct:
use *;
use ;
const CONFIG_DATA: &str = r#"
name = "MyApp"
age = 42
features = ["fast", "reliable"]
"#;
Layered Configuration Example
A common pattern is to load configuration from multiple sources:
use *;
Available Features
| 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 |
Documentation
- 📖 Detailed Usage Guide - Advanced usage, examples, and best practices
- 🔧 API Documentation - Complete API reference
- 💡 Example Code - Example code in the project
Comparison with Other Libraries
I drew inspiration from the following excellent libraries:
Compared to them, Realme's distinguishing features are:
- Unified
serdeConversion: All value conversions are based onserde, enabling seamless type conversion - Flexible Adaptor System: Through
SourceandParsertraits, easily extend support for new data sources and formats - Built-in Hot Reload: File hot-reloading is a first-class, built-in feature
- Profile Support: Native support for multi-environment configuration management
- Convenient Macros: Provides macros to simplify the configuration building process
Contributing
All forms of contributions are welcome! Please feel free to submit a Pull Request.
License
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.