media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
175 stars 57 forks source link

prefix in some nested structs not applied #95

Open kaedwen opened 3 years ago

kaedwen commented 3 years ago

I need to prefix some elements with a namespace therefore I use prefix, but in some cases it is not prepended.

Here is my model

use std::io::Write;
use yaserde::YaSerialize;

#[derive(Debug, Default, YaSerialize)]
#[yaserde(rename = "infrastructure")]
pub struct Infrastructure {
    #[yaserde(attribute)]
    pub id: String,  
    #[yaserde(rename = "scopes", prefix: "aaa")]
    pub scopes: Vec<Scope>,
}

#[derive(Debug, Default, YaSerialize)]
#[yaserde(rename = "scopes", prefix: "aaa")]
pub struct Scope {
    #[yaserde(rename = "scopeRadius", prefix = "aaa")]
    pub scope_radius: Vec<ScopeValue>,
    #[yaserde(rename = "scopeDistance", prefix = "aaa")]
    pub scope_distance: Vec<ScopeValue>,
}

#[derive(Debug, Default, YaSerialize)]
#[yaserde()]
pub struct ScopeValue {
    #[yaserde(attribute, rename = "type")]
    pub r#type: ScopeType, 
    #[yaserde(attribute)]
    pub value: u32, 
}

#[allow(unused)]
#[derive(Debug, PartialEq, YaSerialize)]
#[yaserde()]
pub enum ScopeType {
    #[yaserde(rename = "snap")]
    Snap,
    #[yaserde(rename = "inner")]
    Inner,
    #[yaserde(rename = "outer")]
    Outer
}

impl Default for ScopeType {
    fn default() -> ScopeType {
        ScopeType::Snap
    }
}

and the serialized outcome

<infrastructure id="test123">
    <scopes>
        <aaa:scopeRadius type="snap" value="100" />
        <aaa:scopeRadius type="inner" value="100" />
        <aaa:scopeRadius type="outer" value="500" />
        <aaa:scopeDistance type="snap" value="100" />
        <aaa:scopeDistance type="inner" value="100" />
        <aaa:scopeDistance type="outer" value="300" />
    </scopes>
</infrastructure>

scopeRadius and scopeDistance is prefixed, but I need scopes to be prefix as well

kaedwen commented 3 years ago

This happens when serializing a Vec. Then prefix is ignored somehow.