Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization of enums from config doesn't work #1415

Closed
vorner opened this issue Oct 17, 2018 · 2 comments
Closed

Deserialization of enums from config doesn't work #1415

vorner opened this issue Oct 17, 2018 · 2 comments
Labels

Comments

@vorner
Copy link

vorner commented Oct 17, 2018

Hello

I'm using the config crate together with its serde support. This seems to be problematic together with enums.

Reproducer

In this code, the inner test fails. However, the outer and inner_direct tests pass, so I think I'm not doing something completely silly.

use serde_derive::Deserialize;

#[derive(Deserialize)]
enum X {
    A,
    B,
}

#[derive(Deserialize)]
struct Y {
    x: X,
}

#[derive(Deserialize, Default)]
struct Inner {
    #[serde(default)]
    y: Vec<Y>,
}

#[derive(Deserialize, Default)]
struct Outer {
    #[serde(flatten)]
    y: Inner,
}

#[cfg(test)]
mod tests {
    use super::*;

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

    const TOML: &str = r#"
    [[y]]
    x = "A"

    [[y]]
    x = "B"
    "#;

    fn cfg() -> Config {
        let mut config = Config::new();
        config.merge(File::from_str(TOML, FileFormat::Toml)).unwrap();
        config
    }

    #[test]
    fn inner() {
        cfg().try_into::<Inner>().unwrap();
    }

    #[test]
    fn outer() {
        cfg().try_into::<Outer>().unwrap();
    }

    #[test]
    fn inner_direct() {
        toml::from_str::<Inner>(TOML).unwrap();
    }
}

Exact versions used, if they matter:

  • config: 0.9.1
  • serde: 1.0.80
  • serde-derive: 1.0.80
  • toml: 0.4.8

Guesses

To me it looks like the enum doesn't like to be deserialized from owned String, but doing it from &str is fine. And when going through the flattening, something on the way maybe goes from String to &str. But that's just a wild guess.

@dtolnay
Copy link
Member

dtolnay commented Oct 17, 2018

If inner_direct passes but inner fails, it sounds like this is an issue with the config crate rather than with Serde. Please follow up with them about it. Thanks!

@vorner
Copy link
Author

vorner commented Oct 17, 2018

OK, but still, the fact that Inner and Outer acts different seems strange. Flattening one structure into another as its only field should IMO make them kind of equivalent?

@dtolnay dtolnay added the support label Nov 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants