Skip to main content

mytheclipse_config/
lib.rs

1//! # mytheclipse-config
2//!
3//! A type-safe, dynamic configuration engine: load environment variables,
4//! `.env` files, YAML, JSON, or TOML directly into a typed Rust struct, with
5//! optional runtime hot-reload.
6//!
7//! - [`ConfigLoader`] merges any number of file/env sources into a single
8//!   typed value (files first, environment variables override).
9//! - [`DynamicConfig`] wraps a loaded value behind an `RwLock`, optionally
10//!   watching its source files and swapping in a freshly reloaded value when
11//!   they change, broadcasting the change to subscribers.
12//!
13//! ## Example
14//!
15//! ```no_run
16//! use serde::Deserialize;
17//! use mytheclipse_config::ConfigLoader;
18//!
19//! #[derive(Debug, Deserialize, Clone)]
20//! struct AppConfig {
21//!     port: u16,
22//!     database: DatabaseConfig,
23//! }
24//! #[derive(Debug, Deserialize, Clone)]
25//! struct DatabaseConfig {
26//!     url: String,
27//! }
28//!
29//! let config: AppConfig = ConfigLoader::new()
30//!     .merge_file("config.yaml".as_ref())
31//!     .unwrap()
32//!     .merge_env("APP")
33//!     .build()
34//!     .unwrap();
35//! ```
36
37pub mod error;
38pub mod loader;
39
40#[cfg(feature = "hot-reload")]
41pub mod dynamic;
42
43#[cfg(feature = "schema")]
44pub mod schema;
45
46#[cfg(feature = "validation")]
47pub mod validate;
48
49pub use error::ConfigError;
50pub use loader::ConfigLoader;
51
52#[cfg(feature = "validation")]
53pub use validate::{
54    collect_failures, validate_non_empty, validate_port, validate_range, validate_url,
55    ConfigValidator, ConfigValidatorExt, ValidationError, ValidationFailure,
56};
57
58#[cfg(feature = "hot-reload")]
59pub use dynamic::DynamicConfig;
60
61/// Marker trait for types loadable by [`ConfigLoader`].
62///
63/// Blanket-implemented for any `Deserialize + Send + Sync + 'static`.
64pub trait Config: for<'de> serde::Deserialize<'de> + Send + Sync + 'static {}
65impl<T> Config for T where T: for<'de> serde::Deserialize<'de> + Send + Sync + 'static {}