[Serde-rs] How to deserialize Vec<MyStruct>?

Hello.
I would like deserialize a yaml to the Vec.

#[derive(Debug, Deserialize)]
struct Data {
    #[serde(deserialize_with = "deserialize_enum")]
    fields: Vec<EnumValue>,
}

#[derive(Debug, Serialize, Deserialize, Eq)]
struct EnumValue
{
    id: String,
    value: u8, // enum value: 0..255
    description: String,
}

YAML:

    fields:
      - id: FAKE
        value: 0
        description: Reserve;

https://play.integer32.com/?version=stable&mode=debug&edition=2015&gist=6c6336f1e34016ff2cd25301a140553c

You don't need to do anything special

https://play.integer32.com/?version=stable&mode=debug&edition=2015&gist=d14205524882701b112f9055356cd4fb

The standard derive will correctly deserialize your data

1 Like

Ultimately I want to have an index from Vector in the value field.

    fields:
      - id: FAKE
        description: foo
      - id: FAKE2
        description: bar 
Data { fields: [
     EnumValue { id: "FAKE", value: 0, description: "foo" },
     EnumValue { id: "FAKE2", value: 1, description: "bar" },
 ] }

So something like this? Rust Playground

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.