mehcode/config-rs

ron Deserialize fails with enum with named fields

Closed this issue · 2 comments

Hi,
I'm not sure why but i cannot get enums working with ron.

My test file:

#![cfg(feature = "ron")]

use serde_derive::Deserialize;
use config::{Config, File, FileFormat};

#[derive(Debug, Deserialize)]
enum A {
    VariantA{ port: u16 },
}

#[derive(Debug, Deserialize)]
struct Settings {
    a: A,
}

fn make() -> Config {
    Config::builder()
        .add_source(File::new("tests/test.ron", FileFormat::Ron))
        .build()
        .unwrap()
}

#[test]
fn test_myron() {
    let c = make();

    // Deserialize the entire file as single struct
    let s: Settings = c.try_deserialize().unwrap();

    dbg!(&s);
}

My config file:

(
    a: VariantA(port: 5000)
)

I get the error:

thread 'test_myron' panicked at tests/test_ron.rs:28:43:
called `Result::unwrap()` on an `Err` value: enum A does not have variant constructor port
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

even though serializing the Settings manually to ron gives the same string

Thanks for reporting this!

You need to mark your enum as untagged. I added a testcase for this in #541 - have a look how it is done there!

I see, thanks