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

Unable to Serialize vector struct #214

Closed IskandarAlex2 closed 6 months ago

IskandarAlex2 commented 6 months ago

I'm making sitemap generator and need to add the urls within an array but everytime I do it gave me

Writer: emitter error: last element name is not available
#[derive(Debug, Serialize, PartialEq)]
#[serde(rename = "url")]
pub struct Url {
    pub loc: String,
    pub lastmod: String,
    pub changefreq: String,
    pub priority: f32,
}

#[derive(Debug, Serialize, PartialEq)]
#[serde(rename = "urlset")]
pub struct UrlSet {
    #[serde(rename = "url")]
    pub urls: Vec<Url>,
}
let url1 = Url {
    loc: "https://example.com/page1".to_string(),
    lastmod: "2024-05-19".to_string(),
    changefreq: "weekly".to_string(),
    priority: 0.8,
};

let url2 = Url {
    loc: "https://example.com/page2".to_string(),
    lastmod: "2024-05-18".to_string(),
    changefreq: "monthly".to_string(),
    priority: 0.6,
};

let urlset = UrlSet {
    urls: vec![url1, url2],
};

// Serialize UrlSet to XML string
let xml_string = match to_string(&urlset) {
    Ok(s) => s,
        Err(err) => {
        println!("{}", err);
        return Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
             "Unable to serialize UrlSet to XML",
        )));
    }
};
IskandarAlex2 commented 6 months ago

I gave in a just made a simple writer myself.