serde-xml-rs icon indicating copy to clipboard operation
serde-xml-rs copied to clipboard

Cannot add attribute id

Open jdrouet opened this issue 2 years ago • 0 comments

👋 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?

jdrouet avatar Mar 21 '23 11:03 jdrouet