Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using Environment source to get List of struct #536

Open
Gaelik-git opened this issue Feb 23, 2024 · 1 comment
Open

using Environment source to get List of struct #536

Gaelik-git opened this issue Feb 23, 2024 · 1 comment

Comments

@Gaelik-git
Copy link

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

@matthiasbeyer
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants