How to deserialize a TOML table containing an array of tables

狂风中的少年 提交于 2021-02-11 09:08:31

问题


Take the following TOML data:

[[items]]
foo = 10
bar = 100

[[items]]
foo = 12
bar = 144

And the following rust code:

use serde_derive::Deserialize;
use toml::from_str;
use toml::value::Table;

#[derive(Deserialize)]
struct Item {
    foo: String,
    bar: String
}

fn main() {
    let items_string: &str = "[[items]]\nfoo = 10\nbar = 100\n\n[[items]]\nfoo = 12\nbar = 144\n";
    let items_table: Table = from_str(items_string).unwrap();
    let items: Vec<Item> = items_table["items"].as_array().unwrap().to_vec();
    // Uncomment this line to print the table
    // println!("{:?}", items_table);
}

As you can see by yourself, the program does not compile, giving this error in return:

expected struct Item, found enum toml::value::Value

I understand its meaning, but I don't know how I could solve this and achieve what I wanted to do in the first place: cast a child array of a parent table into an array of structs and NOT into an array of tables.


回答1:


You can parse into the pre-defined TOML types such as Table, but these types don't know about types outside of the pre-defined ones. Those types are mostly used when the actual type of the data is unknown, or unimportant.

In your case that means that the TOML Table type doesn't know about your Item type and cannot be made to know about it.

However you can easily parse into a different type:

use serde_derive::Deserialize;
use std::collections::HashMap;
use toml::from_str;

#[derive(Deserialize, Debug)]
struct Item {
    foo: u64,
    bar: u64,
}

fn main() {
    let items_string: &str = "[[items]]\nfoo = 10\nbar = 100\n\n[[items]]\nfoo = 12\nbar = 144\n";
    let items_table: HashMap<String, Vec<Item>> = from_str(items_string).unwrap();
    let items: &[Item] = &items_table["items"];

    println!("{:?}", items_table);
    println!("{:?}", items);
}

(Permalink to the playground)



来源:https://stackoverflow.com/questions/60592561/how-to-deserialize-a-toml-table-containing-an-array-of-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!