use std::{collections::HashMap, path::PathBuf}; use layeredconf::{Builder, Format, LayeredConf, Source}; use serde::{Deserialize, Serialize}; #[derive(LayeredConf, Deserialize, Serialize, Debug)] struct Config { #[layered(load_config)] #[clap(long)] config: Option, #[clap(long)] name: String, #[clap(long)] port: u16, #[clap(long)] optional: Option, #[clap(long)] vec: Vec, #[layered(subconfig)] twitter: Twitter, #[layered(subconfig)] db: Database, } #[derive(LayeredConf, Deserialize, Serialize, Debug)] #[layered(subconfig)] struct Twitter { auth_token: String, rate_limit: Option, } #[derive(LayeredConf, Deserialize, Serialize, Debug)] #[layered(subconfig)] struct Database { uri: String, #[clap(skip)] options: HashMap, } fn main() -> anyhow::Result<()> { let mut builder = Builder::::new(); builder.new_layer(Source::File( "examples/load_config/lowest.yaml".parse()?, Format::Auto, )); builder.new_layer(Source::File( "examples/load_config/mid.toml".parse()?, Format::Auto, )); builder.new_layer(Source::File( "examples/load_config/highest.json".parse()?, Format::Auto, )); builder.new_layer(Source::FileOptional( "examples/load_config/does_not_exist.json".parse()?, Format::Auto, )); builder.new_layer(Source::Arguments); let solid = builder.solidify()?; println!("{:?}", solid); Ok(()) }