rust-cli / rust-cli/config-rs

Missing field for default value when no environments variable set.

Open
#366 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3.2k
Forks
265
Avg merge
2h 42m
Merged PRs (30d)
4

Description

Greetings!

Got error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: missing field `first`', src/main.rs:55:10
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1805:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1098:23
   4: playground2::main
             at ./src/main.rs:46:18
   5: core::ops::function::FnOnce::call_once
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Process finished with exit code 101

with this code:

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

#[derive(Debug)]
struct Foo {
    secret: Box<str>,
}

impl<'de> Deserialize<'de> for Foo {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        fn default_secret() -> Box<str> {
            "secret0".into()
        }

        #[derive(Debug, Deserialize)]
        struct _Foo {
            first: First,
        }

        #[derive(Debug, Deserialize)]
        struct First {
            second: Second,
        }

        #[derive(Debug, Deserialize)]
        struct Second {
            #[serde(default = "default_secret")]
            secret: Box<str>,
        }

        let foo = _Foo::deserialize(deserializer)?;

        Ok(Self {
            secret: foo.first.second.secret,
        })
    }
}

fn main() {
    //std::env::set_var("FOO_FIRST_SECOND_SECRET", "secret1");
    //std::env::set_var("FOO_FIRST_SECOND_NO", "NO"); // work as expected

    let config = Config::builder()
        .add_source(
            Environment::with_prefix("FOO")
                .try_parsing(true)
                .separator("_"),
        )
        .build()
        .unwrap()
        .try_deserialize::<Foo>()
        .unwrap();

    println!("{config:?}");
}

but if I add some variable (even if it didn't used) in path: std::env::set_var("FOO_FIRST_SECOND_NO", "NO"); , everything work fine.

Any workaround?

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 with the reproduction in src/main.rs, especially Environment::with_prefix and try_deserialize::(). Run it once with no environment variables and once with FOO_FIRST_SECOND_NO set, then inspect the environment-source and nested deserialization behavior. Done means the reported default for secret works consistently when no matching environment variable is present, with a regression test for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.