Config RW - Unified Configuration Management Library
统一配置管理库 - 让配置读写变得简单而强大
🚀 Features | 特性
Multi-source Configuration Priority | 多数据源配置优先级
Command line arguments > Configuration files > Environment variables
命令行参数 > 配置文件 > 环境变量
Type-safe Configuration Access | 类型安全的配置访问
Built-in support for string, integer, float, boolean, and complex JSON types
内置支持字符串、整数、浮点数、布尔值和复杂 JSON 类型
Auto-save and Hot Reload | 自动保存和热重载
Configuration changes are automatically saved to files
配置更改自动保存到文件中
Cross-module Global State | 跨模块全局状态
Share configuration seamlessly across different modules
在不同模块间无缝共享配置
Source Protection | 来源保护
Prevent accidental overwriting of command line and environment variables
防止意外覆盖命令行参数和环境变量
📦 Installation | 安装
Add this to your Cargo.toml:
将以下内容添加到你的 Cargo.toml 文件中:
[]
= "1"
🎯 Quick Start | 快速开始
Basic Usage | 基本使用
use ;
🌟 Advanced Examples | 高级示例
1. Configuration Priority Demo | 配置优先级演示
Demonstrates how different sources override each other
演示不同数据源如何相互覆盖
use ;
2. Cross-Module Configuration | 跨模块配置
Share configuration state across different modules seamlessly
在不同模块间无缝共享配置状态
// main.rs
use ;
// database.rs
use get_string;
// server.rs
use get_i64;
3. Complex Data Types | 复杂数据类型
Handle arrays, objects, and nested configurations with ease
轻松处理数组、对象和嵌套配置
use ;
use ;
4. Auto-save and Source Protection | 自动保存和来源保护
Automatic configuration persistence with intelligent source protection
自动配置持久化和智能来源保护
use ;
🔧 Configuration File Format | 配置文件格式
TOML format with support for nested structures
支持嵌套结构的 TOML 格式
# config.toml
[]
= "My Application"
= "1.0.0"
= false
[]
= true
= true
[]
= "localhost"
= 5432
= "admin"
= "secret"
[]
= 8080
= 4
[[]]
= "server1.com"
= 8080
[[]]
= "server2.com"
= 8080
🎮 Command Line Usage | 命令行使用
Override any configuration value from command line
从命令行覆盖任何配置值
# Basic key-value pairs
# 基本键值对
# Complex nested keys
# 复杂嵌套键
# Boolean and numeric values
# 布尔值和数值
🌍 Environment Variables | 环境变量
Automatic environment variable mapping
自动环境变量映射
# Set environment variables (keys are converted to uppercase with underscores)
# 设置环境变量(键转换为大写并使用下划线)
# Run your application
# 运行你的应用程序
📋 API Reference | API 参考
Initialization | 初始化
// Initialize with configuration file
// 使用配置文件初始化
init_config?;
// Initialize with file path only (no CLI args parsing)
// 仅使用文件路径初始化(不解析命令行参数)
init_config_file_only?;
Reading Values | 读取值
// Type-safe getters
// 类型安全的获取器
let name: = get_string;
let port: = get_i64;
let timeout: = get_f64;
let debug: = get_bool;
// Raw JSON value getter
// 原始 JSON 值获取器
let raw_value: Value = get_arg;
Writing Values | 写入值
// Type-safe setters
// 类型安全的设置器
set_string?;
set_i64?;
set_f64?;
set_bool?;
// Raw JSON value setter
// 原始 JSON 值设置器
set_arg?;
// Save changes to file
// 保存更改到文件
save_config?;
🆚 Comparison with Other Libraries | 与其他库的对比
| Feature | config_rw | config | figment | confy |
|---|---|---|---|---|
| Multi-source Priority | ✅ CLI > File > Env | ❌ | ✅ | ❌ |
| Runtime Modification | ✅ | ❌ | ❌ | ✅ |
| Auto-save | ✅ | ❌ | ❌ | ✅ |
| Type Safety | ✅ | ✅ | ✅ | ✅ |
| Complex Types | ✅ JSON | ✅ Serde | ✅ Serde | ✅ Serde |
| Source Protection | ✅ | ❌ | ❌ | ❌ |
| Global State | ✅ | ❌ | ❌ | ❌ |
多数据源优先级 | ✅ 命令行 > 文件 > 环境变量 | ❌ | ✅ | ❌
运行时修改 | ✅ | ❌ | ❌ | ✅
自动保存 | ✅ | ❌ | ❌ | ✅
类型安全 | ✅ | ✅ | ✅ | ✅
复杂类型 | ✅ JSON | ✅ Serde | ✅ Serde | ✅ Serde
来源保护 | ✅ | ❌ | ❌ | ❌
全局状态 | ✅ | ❌ | ❌ | ❌
🧪 Testing | 测试
Run the comprehensive test suite
运行综合测试套件
# Run all tests with detailed output
# 运行所有测试并显示详细输出
# Run individual test categories
# 运行单独的测试类别
📄 License | 许可证
MIT/Apache-2.0 dual licensed
MIT/Apache-2.0 双重许可
See LICENSE for more details.
详见 LICENSE 文件。
🤝 Contributing | 贡献
Contributions are welcome! Please feel free to submit a Pull Request.
欢迎贡献!请随时提交 Pull Request。
- Fork the repository | Fork 仓库
- Create your feature branch | 创建你的功能分支 (
git checkout -b feature/AmazingFeature) - Commit your changes | 提交你的更改 (
git commit -m 'Add some AmazingFeature') - Push to the branch | 推送到分支 (
git push origin feature/AmazingFeature) - Open a Pull Request | 打开 Pull Request
📧 Contact | 联系方式
Email | 邮箱: guoyumail@qq.com
Project Link | 项目链接: https://github.com/your-username/config_rw
⭐ If this project helps you, please consider giving it a star!
⭐ 如果这个项目对你有帮助,请考虑给它一个星标!