I am writing a code where I am trying to load config.yaml file

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);

Config.yaml file

foo: "test"conf:gggbggbb

Output

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

I have kept the config.yaml file in the same folder where it is getting called. It looks like it is not able to load the file itself. What is getting missed there?

EDIT: When I changed the extension from yaml to toml, and provided full path, it found the file but the structure is expecting is

config.toml

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

full path

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

Tried multiple places to keep it but not getting it, looks like it requires full path.

But I got the output

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

Best Answer


While David Chopin's answer is correct that the YAML is not right, there is a couple of deeper issues.

Firstly, while it is not really documented, looking at the confy source, it expects TOML formatted data, not YAML - for simple cases they can be similar I think. (Turns out this is not 100% correct - the github page says you can switch to YAML this using features = ["yaml_conf"] in the Cargo.toml file)

Secondly, I'm guessing the root problem is that confy is not finding your configuration file.

The docs for confy::load state:

Load an application configuration from disk

A new configuration file is created with default values if none exists.

So, I think it's looking somewhere else, not finding your file and instead of erroring creating a nice default file in that location then returning that default for you.

I believe it should be formatted as follows:

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