mehcode / config-rs

⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

using Environment source to get List of struct

Gaelik-git opened this issue · comments

Hello I would like to know if it's actually possible to have this Settings::new return a Ok result

use config::{Config, ConfigError, Environment};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[allow(unused)]
struct Auth {
    token: String,
    secret: String,
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Settings {
    auths: Vec<Auth>,
}

impl Settings {
    pub fn new() -> Result<Self, ConfigError> {
        let s = Config::builder()
            .add_source(
                Environment::with_prefix("APP")
                    .separator("_")
                    .try_parsing(true),
            )
            .build()?;

        s.try_deserialize()
    }
}

fn main() {
    let settings = Settings::new();

    // Print out our settings
    println!("{:?}", settings);
}

I was thinking something like APP_AUTHS_0_TOKEN=token APP_AUTHS_0_SECRET=secret cargo run
I found the list_separator method on Environment but I think it's only to manage Vec<String>

Is it something that is currently possible ? If not is it a feature that would make sense to implement or maybe I should use another Source ?

Thanks

Hi, I think something like that is not possible right now.