hocon.rs icon indicating copy to clipboard operation
hocon.rs copied to clipboard

Error deserializing: missing field when chaining load_file&resolve

Open david-bouyssie opened this issue 3 years ago • 2 comments

I'm trying to use the following code to load a config:

    let tmp_conf: TestConfig = HoconLoader::new()
        .load_file("./tmp.conf")?
        .resolve()?;

But I get the following error: Error deserializing: ".: missing field `child2`"

Here is the config file:

title = "test"

child1 {
  home = "./dir1"
}

child2 {
  home = "./dir2"
}

Here are the defined structs:

#[derive(Clone, Deserialize)]
pub struct TestConfig {
    pub title: String,
    pub child1: Child1,
    pub child2: Child2
}

#[derive(Clone, Deserialize)]
pub struct Child1 {
    pub home: String,
}

#[derive(Clone, Deserialize)]
pub struct Child2 {
    pub home: String,
}

Important note, using from_str directly works without error:

 let s = r#"{
      title = "test"
      
      child1 {
        home = "./dir1"
      }
      
      child2 {
        home = "./dir2"
      }
    }"#;

let config: TestConfig = hocon::de::from_str(s)?;

david-bouyssie avatar Apr 13 '21 13:04 david-bouyssie

I forgot to say that I was testing on Windows. It seems to be a line separator issue.

If I force Unix line separator (LF), then the error goes away.

david-bouyssie avatar Apr 13 '21 13:04 david-bouyssie

So a possible workaround until a fix is:

    let s = std::fs::read_to_string("./tmp.conf")?.replace('\r', "");
    let tmp_conf: TestConfig = hocon::de::from_str(&*s)?;

david-bouyssie avatar Apr 13 '21 13:04 david-bouyssie