Skip to main content

from_config

Function from_config 

Source
pub fn from_config<T: VldParse>(config: &Config) -> Result<T, VldConfigError>
Expand description

Load and validate configuration from a config::Config instance.

The config is first deserialized into a serde_json::Value, then validated and parsed using the vld schema via VldParse.

§Errors

Returns VldConfigError::Source if the config cannot be deserialized into JSON, or VldConfigError::Validation if validation fails.

§Example

use vld::prelude::*;

vld::schema! {
    #[derive(Debug)]
    pub struct Settings {
        pub host: String => vld::string().min(1),
        pub port: i64    => vld::number().int().min(1).max(65535),
    }
}

let config = config::Config::builder()
    .add_source(config::File::with_name("config"))
    .build()
    .unwrap();

let settings: Settings = vld_config::from_config(&config).unwrap();