Skip to main content

read

Function read 

Source
pub fn read<P, S, T>(source: ConfigSource<P, S>) -> Result<T, Error>
Expand description

Read config from an explicit source.

ConfigSource::File keeps the historical behavior of creating the file from T::default() when it does not exist.

Environment variables map to fields by lowercasing the suffix after prefix. Use __ for nested fields and provide JSON literals for typed values such as arrays and booleans.

use cloudiful_config::{ConfigSource, read};
use serde::{Deserialize, Serialize};

#[derive(Default, Deserialize, Serialize)]
struct DatabaseConfig {
    url: String,
}

#[derive(Default, Deserialize, Serialize)]
struct AppConfig {
    database: DatabaseConfig,
}

let path = std::env::temp_dir().join("config-crate-read-with-env.toml");
unsafe {
    std::env::set_var("APP_DATABASE__URL", "\"postgres://db/service\"");
}
let _config: AppConfig = read(ConfigSource::FileWithEnv {
    path: &path,
    prefix: "APP_",
})
.unwrap();
unsafe {
    std::env::remove_var("APP_DATABASE__URL");
}