Help with vec in serde

I have a problem with using a Vec of structs in deserializing a big xml.

I made a small example program and was able to deserialize a small and simple xml using a vec exactly the same way, so I'm confused why it does not work in my bigger project.
My struct looks like this:


#[derive(Debug, PartialEq, Clone, Deserialize)]
#[serde(tag = "DataVersion")]
pub enum Gdtf {
    #[serde(rename(deserialize = "1.0"))]
    Version1_0(GdtfV1),
    #[serde(rename(deserialize = "1.1"))]
    Version1_1(GdtfV1),
}

#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct GdtfV1 {
    #[serde(rename = "FixtureType")]
    pub fixture_type: FixtureType,
}

#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct FixtureType {
    #[serde(rename = "Name")]
    pub name: Name,
    #[serde(rename = "ShortName")]
    pub short_name: String,
    #[serde(rename = "LongName")]
    pub long_name: String,
    #[serde(rename = "Manufacturer")]
    pub manufacturer: String,
    #[serde(rename = "Description")]
    pub description: String,
    #[serde(rename = "FixtureTypeID")]
    pub fixture_type_id: Guid,
    #[serde(rename = "Thumbnail")]
    pub thumbnail: Resource,
    #[serde(rename = "RefFT")]
    pub ref_ft: GuidOpt,
    #[serde(rename = "CanHaveChildren", default)]
    pub can_have_children: CanHaveChildren,
    #[serde(rename = "AttributeDefinitions")]
    pub attribute_definitions: AttributeDefinitions,
}


#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct AttributeDefinitions {
    #[serde(rename = "FeatureGroups")]
    pub feature_groups: FeatureGroups,
}

#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct FeatureGroups {
    #[serde(rename = "FeatureGroup")]
    pub feature_group: Vec<FeatureGroup>,
}

#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct FeatureGroup {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "Pretty")]
    pub pretty: String
}

There is an error Error: DeError(Custom("invalid type: map, expected a sequence")) caused by the Vec in FeatureGroups when deserializing this xml:

 <FixtureType Description="ACME AE-610 BEAM" FixtureTypeID="E62F2ECF-2A08-491D-BEEC-F5C491B89784" LongName="ACME AE 610 BEAM" Manufacturer="ACME" Name="ACME AE-610 BEAM" RefFT="8F54E11C-4C91-11E9-80BC-F1DFE217E634" ShortName="ACME AE 610 BEAM" Thumbnail="AE-610 BEAM">
    <AttributeDefinitions>
      <ActivationGroups>
        <ActivationGroup Name="PanTilt"/>
        <ActivationGroup Name="Gobo1"/>
        <ActivationGroup Name="Prism"/>
        <ActivationGroup Name="ColorRGB"/>
      </ActivationGroups>
      <FeatureGroups>
        <FeatureGroup Name="Position" Pretty="Position">
          <Feature Name="PanTilt"/>
        </FeatureGroup>
        <FeatureGroup Name="Gobo" Pretty="Gobo">
          <Feature Name="Gobo"/>
        </FeatureGroup>

Here is a link to the whole project:

Can someone help me please?

I think this is due to serdes enum representations is not working as expected · Issue #190 · tafia/quick-xml · GitHub / Internal buffering disrupts format-specific deserialization features · Issue #1183 · serde-rs/serde · GitHub
I don't think it's related to vecs but the Gdtf enum. If you try to deserialize the XML as GdtfV1 directly, it works. Not sure what the best workaround is in this case.

2 Likes

You were right! Thanks!