Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add attribute id #200

Open
jdrouet opened this issue Mar 21, 2023 · 1 comment
Open

Cannot add attribute id #200

jdrouet opened this issue Mar 21, 2023 · 1 comment

Comments

@jdrouet
Copy link

jdrouet commented Mar 21, 2023

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

@YsuOS
Copy link

YsuOS commented Dec 14, 2024

PR for this issue #218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants