Skip to main content

AppConfig

Trait AppConfig 

Source
pub trait AppConfig:
    DeserializeOwned
    + Validate
    + Send
    + Sync
    + 'static {
    // Required methods
    fn apply_defaults(&mut self);
    fn service_config(&self) -> &ServiceConfig;
}
Expand description

Trait that every application config struct must implement.

Typically implemented by a struct that embeds ServiceConfig and adds service-specific fields.

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 }
}

let cfg: MyConfig = ConfigLoader::app().load_app()?;
assert_eq!(cfg.api_token.to_string(), "***");

Required Methods§

Source

fn apply_defaults(&mut self)

Apply any programmatic defaults after deserialization.

Source

fn service_config(&self) -> &ServiceConfig

Return a reference to the embedded ServiceConfig.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§