Config Manager
A thread-safe configuration manager for Rust applications that loads and caches JSON configuration files.
Features
- Thread-safe caching: Configurations are loaded once and cached for subsequent accesses
- Concurrent access: Safe for use across multiple threads with
RwLocksynchronization - Type-safe retrieval: Supports deserializing configuration values into Rust types
- JSON-based: Configuration files are stored in JSON format
Usage
Basic Usage
- Create a JSON configuration file in the
configs/directory (e.g.,configs/app.json):
- Use the configuration in your Rust code:
use Config;
let config = new;
let db_host: String = config.get.unwrap;
let db_port: u16 = config.get.unwrap;
let debug_mode: bool = config.get.unwrap;
Advanced Usage
// Create multiple configuration instances
let app_config = new;
let db_config = new;
// Get nested values (if your JSON has nested objects)
let nested_value = app_config..unwrap;
// Handle missing values
match app_config.
API Reference
Config::new(name: &str) -> Config
Creates a new configuration instance for the given name. The configuration is loaded from configs/{name}.json if not already cached.
config.get<T: DeserializeOwned>(key: &str) -> Option<T>
Retrieves a configuration value by key, attempting to deserialize it into type T.
Thread Safety
The configuration manager is designed for concurrent access:
- Configuration loading is synchronized
- Multiple threads can read configurations simultaneously
- Configuration updates are exclusive (blocking other accesses during write)
Example Configuration File
Contributing
Contributions are welcome! Please open an issue or submit a PR for:
- New features
- Performance improvements
- Bug fixes
License
Dual-licensed under MIT or Apache 2.0 at your option.