use std::io::Write;
use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use quick_xml::events::BytesText;
use quick_xml::writer::Writer;
pub(super) fn write_xml<W: Write>(links: Vec<String>, w: &mut W) -> Result<()> {
let mut writer = Writer::new_with_indent(w, b' ', 2);
writer
.write_bom()
.context("[write_xml] Failed to write byte-order-marks to the XML document.")?;
writer
.get_mut()
.write_all(b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.context("[write_xml] Failed to write to the XML document.")?;
writer
.create_element("urlset")
.with_attribute(("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"))
.write_inner_content(|writer| {
for link in links.iter() {
writer
.create_element("url")
.write_inner_content(|w| {
w.create_element("loc")
.write_text_content(BytesText::new(link.as_str()))
.context(
"[write_xml] Failed to write <loc> element to the XML document.",
)?;
Ok::<_, Error>(())
})
.context("[write_xml] Failed to write <url> element to the XML document.")?;
}
Ok::<_, Error>(())
})?;
Ok::<_, Error>(())
}
#[cfg(test)]
mod test {
}