在下面的YAML文件中:
// 1. book yaml
kind: book
title: Someone's life
publisher: someone
published_at: 2023-02-24T09:31:00Z+09:00
// 2. movie yaml
kind: movie
title: Someone's life
director: someone else
kind
是认识到YAML是什么的关键.如果种类是book
,这件事就有publisher
和published_at
.然而,如果善良是movie
,这件事就不是publisher
而是director
.
在我的简单 idea 中,为了解析它,我必须创建通用的解析 struct . 例如:
#[derive(Serialize, Deserialize, Debug)]
struct General {
kind: String,
title: String,
}
使用General struct解析一次,然后获得General类型的解析,然后使用另一个 struct 重新解析. 例如:
#[derive(Serialize, Deserialize, Debug)]
struct Book {
kind: String,
title: String,
publisher: String,
published_at: String,
}
上面的场景可以工作,但我必须解析两次.但是,这是有效的吗?