0

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:
        gg
        gb
        gg
        bb

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",
    ],
}
Harshit
  • 5,147
  • 9
  • 46
  • 93

2 Answers2

1

I believe it should be formatted as follows:

foo: "test"
conf:
        - gg
        - gb
        - gg
        - bb
David Chopin
  • 2,780
  • 2
  • 19
  • 40
  • It is giving the same empty result – Harshit Oct 06 '20 at 04:21
  • Hmm, I'm not sure then. Source: https://stackoverflow.com/questions/23657086/yaml-multi-line-arrays/33136212 – David Chopin Oct 06 '20 at 04:22
  • Looks like it is not expecting yaml file, only toml ones, but when I searched for yml library, I found confy so trying the same. Also it was not able to load the config file, I gave full path, then it loaded. Thanks for your time – Harshit Oct 06 '20 at 04:40
1

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.

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • Yes, I thought to be using yaml since I searched for this but it is expecting toml file with the same structure, but it is not able to load file in same folder without path, I had to give full path to load it. Thanks for you time – Harshit Oct 06 '20 at 04:42