dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
965 stars 164 forks source link

Deserialize of enum breaks in latest release on upgrade from 0.8 #305

Closed vikigenius closed 2 years ago

vikigenius commented 2 years ago

I have the following snippet of code

use serde_yaml; // 0.8.24
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub enum WidgetProps {
    Text(TextProps)
}

#[derive(Deserialize, Default, Debug)]
struct Padding {
    h: u8,
    w: u8,
}

#[derive(Deserialize, Debug)]
pub struct StyleProps {
    fg_color: Option<String>,
    bg_color: Option<String>,
    #[serde(default)]
    padding: Padding, // Use default if unavailable
}

#[derive(Deserialize, Debug)]
pub struct TextProps {
    text: String,
    #[serde(flatten)]
    style_props: Option<StyleProps>,
}

fn main() {
    let yaml_string = r#"
Text:
  text: "Example"
  fg_color: "blue"
  bg_color: "black"
"#;
    let widget: WidgetProps = serde_yaml::from_str(yaml_string).unwrap();
    dbg!(widget);
}

This works fine with the 0.8 version of serde_yaml. But on upgrading to 0.9 versions (latest) it panics saying

Error("invalid type: map, expected a YAML tag starting with '!'", line: 2, column: 1)'
vikigenius commented 2 years ago

Ok, I just got it, the new version forces the usage of tags. So this works

fn main() {
    let yaml_string = r#"
!Text
  text: "Example"
  fg_color: "blue"
  bg_color: "black"
"#;
    let widget: WidgetProps = serde_yaml::from_str(yaml_string).unwrap();
    dbg!(widget);
}
dtolnay commented 2 years ago

Glad you figured it out!