RReverser / serde-xml-rs

xml-rs based deserializer for Serde (compatible with 1.0+)
https://crates.io/crates/serde-xml-rs
MIT License
273 stars 92 forks source link

Cannot add attribute id #200

Open jdrouet opened 1 year ago

jdrouet commented 1 year ago

šŸ‘‹ Hi everyone! I'm trying to serialize a list of categories like following

#[derive(Debug, serde::Serialize)]
#[serde(rename = "categories")]
pub struct Categories {
    #[serde(rename = "$value")]
    inner: [Category; 2],
}

impl Default for Categories {
    fn default() -> Self {
        Self {
            inner: [Category::new(1, "Animal"), Category::new(2, "Vehicle")],
        }
    }
}

#[derive(Debug, serde::Serialize)]
#[serde(rename = "category")]
pub struct Category {
    #[serde(rename = "@id")]
    id: u32,
    #[serde(rename = "@name")]
    name: &'static str,
}

impl Category {
    pub const fn new(id: u32, name: &'static str) -> Self {
        Self { id, name }
    }
}

#[test]
fn should_serialize() {
    let cats = Categories::default();
    serde_xml_rs::to_string(&cats).unwrap();
}

But it ends up with the following error

---- test::should_serialize stdout ----
thread 'test::should_serialize' panicked at 'called `Result::unwrap()` on an `Err` value: Custom { field: "Cannot add attribute id" }', src/test.rs:34:36
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I don't have the impression to do some weird things but it doesn't work... do you have any idea of what could cause this in my code?