2026-08-28 22:24:14 +07:00
|
|
|
# mytheclipse-config
|
2026-08-28 21:45:40 +07:00
|
|
|
|
|
|
|
|
Type-safe, dynamic configuration: load `.env`, YAML, JSON, or TOML directly
|
|
|
|
|
into a typed Rust struct, merge multiple sources with environment variables
|
|
|
|
|
taking priority, and optionally hot-reload when the source files change.
|
|
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
|
|
- `env` (default) — `.env` file loading via `dotenvy`.
|
|
|
|
|
- `yaml` / `toml` (default) — structured file parsing (`.json` is always available).
|
|
|
|
|
- `hot-reload` (default) — watch files and swap in a freshly reloaded value.
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
use serde::Deserialize;
|
2026-08-28 22:24:14 +07:00
|
|
|
use mytheclipse_config::ConfigLoader;
|
2026-08-28 21:45:40 +07:00
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
struct AppConfig {
|
|
|
|
|
port: u16,
|
|
|
|
|
database_url: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let config: AppConfig = ConfigLoader::new()
|
|
|
|
|
.merge_file("config.yaml".as_ref())?
|
|
|
|
|
.merge_env("APP") // APP_PORT, APP_DATABASE_URL override the file
|
|
|
|
|
.build()?;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Hot-reload
|
|
|
|
|
|
|
|
|
|
```rust
|
2026-08-28 22:24:14 +07:00
|
|
|
use mytheclipse_config::DynamicConfig;
|
2026-08-28 21:45:40 +07:00
|
|
|
# use serde::Deserialize;
|
|
|
|
|
# #[derive(Debug, Deserialize, Clone)] struct AppConfig { port: u16 }
|
|
|
|
|
|
|
|
|
|
let cfg = DynamicConfig::<AppConfig>::watch_files(
|
|
|
|
|
vec!["config.yaml".into()],
|
2026-08-28 22:24:14 +07:00
|
|
|
|| mytheclipse_config::ConfigLoader::new().merge_file("config.yaml".as_ref())?.build(),
|
2026-08-28 21:45:40 +07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let mut changes = cfg.subscribe();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
while changes.recv().await.is_ok() {
|
|
|
|
|
println!("config reloaded: {:?}", cfg.get());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
```
|