I have a map of items that I would like to serialize to a list of structs, each having a field for the corresponding key.
Imagine having a YAML file like this:
name_a:
some_field: 0
name_b:
some_field: 0
name_c:
some_field: 0
And a corresponding structure like this:
struct Item {
name: String,
some_field: usize,
}
I would like to deserialize the named items into a Vec<Item> instead of a Map<String, Item>. The item names (name_a, ...) are put into the name field of the Item objects.
I've attempted the following:
extern crate serde_yaml;
use std::fs::read_to_string;
let contents = read_to_string("file.yml").unwrap();
let items: Vec<Item> = serde_yaml::from_str(&contents).unwrap();
This however doesn't work and produces the invalid type: map, expected a sequence error.
I'd prefer to avoid creating a transient Map<String, PartialItem> that is converted to a Vec, and I would also prefer not to implement an additional PartialItem struct. Using an Option<String> as name would be possible, although I don't think this is optimal.