Deserializing TOML into vector of enum with values

人走茶凉 提交于 2019-12-01 08:13:39

Serde has lots of options for serializing enums.

One that works for your case:

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml;

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", content = "args")]
enum Actions {
    Wait(usize),
    Move { x: usize, y: usize },
}

fn main() {
    println!("{}", toml::to_string(&Actions::Wait(5)).unwrap());

    println!("{}", toml::to_string(&Actions::Move { x: 1, y: 1 }).unwrap());
}
type = "Wait"
args = 5
type = "Move"

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