1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::sitemap::Sitemap;
use crate::sitemap_index_error::SitemapIndexError;
use crate::{ENCODING, NAMESPACE};
use std::io::Write;
use xml_builder::{XML, XMLBuilder, XMLElement, XMLError, XMLVersion};
/// Encapsulates information about all the Sitemaps in the file.
pub struct SitemapIndex {
/// The XML version.
pub xml_version: XMLVersion,
/// The XML encoding.
pub xml_encoding: String,
/// The namespace for the \<sitemapindex\>.
pub xmlns: String,
/// All the sitemaps that will become indexed.
pub sitemaps: Vec<Sitemap>,
}
impl SitemapIndex {
/// # Errors
///
/// Will return `SitemapIndexError::TooManySitemaps` if the length of `sitemaps` is above `50,000`.
pub fn new(sitemaps: Vec<Sitemap>) -> Result<Self, SitemapIndexError> {
// SitemapIndex cannot contain more than 50,000 sitemaps
if sitemaps.len() > 50_000 {
return Err(SitemapIndexError::TooManySitemaps(sitemaps.len()));
}
Ok(Self {
xml_version: XMLVersion::XML1_0,
xml_encoding: ENCODING.to_string(),
xmlns: NAMESPACE.to_string(),
sitemaps,
})
}
/// # Errors
///
/// Will return `XMLError` if there is a problem creating XML elements.
pub fn to_xml(self) -> Result<XML, XMLError> {
// create XML document
let mut xml = XMLBuilder::new()
.version(self.xml_version)
.encoding(self.xml_encoding)
.build();
// create <sitemapindex>
let mut sitemap_index: XMLElement = XMLElement::new("sitemapindex");
sitemap_index.add_attribute("xmlns", self.xmlns.as_str());
// add each <sitemap>
for sitemap in self.sitemaps {
sitemap_index.add_child(sitemap.to_xml()?)?;
}
// set root element and we're done!
xml.set_root_element(sitemap_index);
Ok(xml)
}
/// # Errors
///
/// Will return `XMLError` if there is an IO Error dealing with the
/// underlying writer or if there is an error generating XML.
pub fn write<W: Write>(self, writer: W) -> Result<(), XMLError> {
let xml: XML = self.to_xml()?;
xml.generate(writer)
}
}