首页 > 解决方案 > 使用数组类型加载 yaml 配置

问题描述

我正在编写一个代码,我试图加载 config.yaml 文件

impl ::std::default::Default for MyConfig {
    fn default() -> Self { Self { foo: "".into(), conf: vec![] } }
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MyConfig {
    foo: String,
    conf: Vec<String>
}

let cfg: MyConfig = confy::load("config")?;
println!("{:#?}", cfg);

配置.yaml 文件

foo: "test"
conf:
        gg
        gb
        gg
        bb

输出

MyConfig {
    url: "",
    jobs: [],
}

我将 config.yaml 文件保存在调用它的同一文件夹中。看起来它无法加载文件本身。那里错过了什么?

编辑:当我将扩展名从 yaml 更改为 toml 并提供完整路径时,它找到了文件,但预期的结构是

配置文件

foo = "test"
conf = ["gg","gb","gv","gx"]

完整路径

confy::load("c:/test/config")?;

尝试了多个地方来保留它但没有得到它,看起来它需要完整路径。

但我得到了输出

MyConfig {
    url: "test",
    jobs: [
        "gg",
        "gb",
        "gv",
        "gx",
    ],
}

标签: rust

解决方案


我认为它应该格式化如下:

foo: "test"
conf:
        - gg
        - gb
        - gg
        - bb

推荐阅读