rskit_config/lib.rs
1//! Adapter-oriented configuration loading with validation.
2//!
3//! # Example
4//!
5//! The end-to-end app example below exercises the `validate`-gated App API (`AppConfig`/`ServiceConfig`/`Validate`),
6//! so it only compiles when the default `validate` feature is enabled.
7//!
8#![cfg_attr(feature = "validate", doc = "```no_run")]
9#![cfg_attr(not(feature = "validate"), doc = "```ignore")]
10//! use rskit_config::{AppConfig, ConfigLoader, SecretString, ServiceConfig};
11//! use rskit_validation::Validate; use serde::Deserialize;
12//!
13//! #[derive(Debug, Deserialize)] struct MyConfig {
14//! #[serde(flatten)]
15//! service: ServiceConfig,
16//! grpc_port: u16,
17//! api_token: SecretString,
18//! }
19//!
20//! impl Validate for MyConfig {
21//! fn validate(&self) -> Result<(), validator::ValidationErrors> {
22//! self.service.validate()?;
23//! if self.grpc_port == 0 {
24//! let mut errors = validator::ValidationErrors::new();
25//! errors.add("grpc_port", validator::ValidationError::new("range"));
26//! return Err(errors);
27//! }
28//! Ok(())
29//! }
30//! }
31//!
32//! impl AppConfig for MyConfig {
33//! fn apply_defaults(&mut self) {
34//! if self.grpc_port == 0 {
35//! self.grpc_port = 50051;
36//! }
37//! }
38//!
39//! fn service_config(&self) -> &ServiceConfig {
40//! &self.service
41//! }
42//! }
43//!
44//! # fn main() -> rskit_errors::AppResult<()> {
45//! let cfg: MyConfig = ConfigLoader::app()
46//! .with_default("grpc_port", 50051_i64)
47//! .with_env_prefix("MYAPP")
48//! .load_app()?;
49//! assert_eq!(cfg.api_token.to_string(), "***");
50//! # Ok(())
51//! # }
52//! ```
53
54#![warn(missing_docs)]
55
56#[cfg(feature = "validate")]
57mod app;
58#[cfg(feature = "validate")]
59mod service;
60mod sink;
61mod source;
62mod strict;
63mod typed;
64#[cfg(feature = "watch")]
65mod watch;
66
67#[cfg(feature = "validate")]
68pub use app::AppConfig;
69pub use rskit_util::SecretString;
70#[cfg(feature = "validate")]
71pub use service::{Environment, LogFormat, LogOutput, LoggingConfig, ServiceConfig};
72pub use sink::{ConfigSink, ConfigTable, FileConfigSink, InMemoryConfigSink};
73#[cfg(feature = "validate")]
74pub use source::load_config;
75pub use source::{
76 ConfigLoader, ConfigMapSource, ConfigSource, DotenvFileSource, EnvironmentSource, Profile,
77 TomlFileSource,
78};
79pub use strict::{
80 CompositeKey, IdentityKey, IncludeMerge, MergeIdentity, RawTable, RawValue, StrictLoader,
81 deserialize_subtree, load_strict,
82};
83#[cfg(feature = "watch")]
84pub use watch::{ConfigChange, ConfigChangeStream, ConfigWatch};