rust-cli / rust-cli/config-rs

using Environment source to get List of struct

Open
#536 6 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-env C-enhancement S-waiting-on-design
Dominant language
Rust
Stars
3.2k
Forks
265
Avg merge
2h 42m
Merged PRs (30d)
4

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Environment source configuration, especially list_separator and try_parsing, then trace how Settings::new calls build and try_deserialize. Determine whether APP_AUTHS_0_TOKEN and APP_AUTHS_0_SECRET can produce Vec; done means either documenting the supported approach or defining and implementing a decided way to deserialize lists of structs.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.