bnjjj / twelf

Twelf is a configuration solution for Rust including 12-Factor support. It is designed with layers in order to configure different sources and formats to build your configuration. The main goal is to be very simple using a proc macro.
MIT License
112 stars 13 forks source link

Incorrect behavior with struct flattening #35

Open Shatur opened 1 year ago

Shatur commented 1 year ago

Let's consider the following test:

#[test]
fn mixed_clap_flatten_defaults() {
    #[config]
    #[derive(Parser, Debug, Default)]
    #[clap(author, version, about, long_about = None)]
    struct Conf {
        #[clap(flatten)]
        package: Package,
    }

    #[derive(Args, Default, Debug, serde::Serialize, serde::Deserialize)]
    struct Package {
        #[clap(long, required = false)]
        edition: String,
    }

    let matches = Conf::command().get_matches_from(&["test", "--edition=2000"]);

    let prio = vec![Layer::Toml("Cargo.toml".into()), Layer::Clap(matches)];
    let config = Conf::with_layers(&prio).unwrap();

    assert_eq!(config.package.edition, "2000");
}

It panics at assert_eq!(config.package.edition, "2000");. I would expect CLI to override the value from file. But it prints 2021 because due to #[clap(flatten)] it incorrectly merges values from clap.

0xForerunner commented 3 weeks ago

+1 on this issue! It' a blocker for me to use this lib unfortunately.