rust-cli / confy

šŸ›‹ Zero-boilerplate configuration management in Rust
Other
907 stars 61 forks source link

Weird behavior using Vec #15

Open gdetrez opened 5 years ago

gdetrez commented 5 years ago

I'm using confy 0.3.1, and it seems that I am unable to remove items from a vec in my config (adding items works fine). But when I was trying to build a simple example to demonstrate the issue, I found a very confusing one.

Here's my code:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct MyItem {
    name: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct MyConfig {
    items: Vec<MyItem>,
}

/// `MyConfig` implements `Default`
impl ::std::default::Default for MyConfig {
    fn default() -> Self {
        Self {
            items: vec![MyItem { name: "foo".into() }, MyItem { name: "bar".into() }],
        }
    }
}

fn main() -> Result<(), ::std::io::Error> {
    let mut cfg: MyConfig = confy::load("foobar")?;
    println!("{:?}", cfg);
    cfg.items.remove(0);
    println!("{:?}", cfg);
    confy::store("foobar", cfg)?;
    Ok(())
}

This is what I get in the config file after running it (any number of times):

[[items]]
name = 'bar'

[[items]]
name = 'bar'

I expected one item after the first run and zero after subsequent runs. And the default vec has two different items (foo and bar) whereas the toml file have the same item twice.

gdetrez commented 5 years ago

After a bit more investigation, I now believe this is the same issue as #9: bits of the "old" config remain when storing the modified config. And if I switch to the git repo in Cargo.toml, the example works as expected. I didn't notice the similarity at first because in my tests the config file remained syntactically valid.

Alch-Emi commented 5 years ago

Seems like this was solved alongside #ā€‹9 in commit 44c6d52. Reproduces in 6a0f409, but not after #ā€‹9 was merged. Should be fixed with the next release?

tristanmagne commented 4 years ago

any update on this issue?