Skip to content

How to merge sources with existing default values? #628

@cbeuw

Description

@cbeuw

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions