【发布时间】:2020-05-14 09:07:40
【问题描述】:
我有以下 YAML:
build:
- step 1
- step 2
- name: step 3
do: something
- name: step 4
get: fetch ...
- name: step 5
put: upload something
为了解析它,我正在尝试这个
#[derive(Debug, Deserialize, PartialEq)]
struct Config {
build: Option<Vec<Steps>>
}
#[derive(Debug, Deserialize, PartialEq)]
struct Build {
#[serde(flatten)]
item: String,
steps: Steps,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Steps {
name: String,
r#do: Option<String>,
put: Option<String>,
get: Option<String>,
}
但我收到此错误:
Error parsing configuration file: build[0]: invalid type: string "step 1", expected struct Build
我只能解析:
build:
- step 1
- step 2
使用这个:
#[derive(Debug, Deserialize, PartialEq)]
struct Config {
build: Option<Vec<String>>
}
或者:
build:
- name: step 1
do: foo
- name: step 2
do: bar
使用:
#[derive(Debug, Deserialize, PartialEq)]
struct Config {
build: Option<Vec<Steps>>
}
但是找不到同时拥有这两种选择的方法,知道如何解析吗?
【问题讨论】:
-
minimal reproducible example ?用的板条箱?使用的版本?代码 ?你是新来的?我们会猜测吗?
-
嗨@Stargateur,我认为标签[
serde,yaml,rust] 可以有所帮助,并不是真的希望您猜到,但可以利用标签,可以提供帮助。我会更新问题,谢谢你的提示。