1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Service-application config convenience trait.
use crateServiceConfig;
/// Trait that every application config struct must implement.
///
/// Typically implemented by a struct that embeds [`ServiceConfig`] and adds service-specific fields.
///
/// ```no_run
/// use rskit_config::{AppConfig, ConfigLoader, SecretString, ServiceConfig};
/// use rskit_validation::Validate;
///
/// #[derive(serde::Deserialize)]
/// struct MyConfig {
/// #[serde(flatten)]
/// service: ServiceConfig,
/// grpc_port: u16,
/// api_token: SecretString,
/// }
///
/// impl Validate for MyConfig {
/// fn validate(&self) -> Result<(), validator::ValidationErrors> {
/// self.service.validate()?;
/// if self.grpc_port == 0 {
/// let mut errors = validator::ValidationErrors::new();
/// errors.add("grpc_port", validator::ValidationError::new("range"));
/// return Err(errors);
/// }
/// Ok(())
/// }
/// }
///
/// impl AppConfig for MyConfig {
/// fn apply_defaults(&mut self) {
/// if self.grpc_port == 0 { self.grpc_port = 50051; }
/// }
/// fn service_config(&self) -> &ServiceConfig { &self.service }
/// }
///
/// # fn main() -> rskit_errors::AppResult<()> {
/// let cfg: MyConfig = ConfigLoader::app().load_app()?;
/// assert_eq!(cfg.api_token.to_string(), "***");
/// # Ok(())
/// # }
/// ```