rust-cli / rust-cli/config-rs

How to merge sources with existing default values?

Open
#628 4 comments 6 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

Suppose I have this config struct

#[derive(Debug, Deserialize)]
struct MyConfig {
    // ...
}

impl Default for MyConfig {
    fn default() -> MyConfig {
        // some complex logic
    }
}

I want to produce a MyConfig, where values explicitly set in config.toml are used, otherwise, the default implementation is used.

Something like

Config::builder().
    .add_source(File::with_name("config.toml"))
    .set_default(???, MyConfig::default())
    .build()?;

But I couldn't figure out a way to do this. This seems like a very common use case, so I feel like I'm missing something.

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 Config::builder, File::with_name, set_default, and build APIs referenced in the issue. Determine whether the existing source and default-value mechanisms can express a MyConfig-wide fallback, and define the expected behavior for explicitly configured versus defaulted fields.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.