Struct sitemap_xml_writer::SitemapIndexWriter
source · pub struct SitemapIndexWriter<W: Write> { /* private fields */ }
Expand description
A writer for sitemap index file.
Examples
The following example is a sitemap index containing only one URL specified by &str
.
use sitemap_xml_writer::{SitemapIndexWriter, Sitemap};
use std::io::Cursor;
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write("http://www.example.com/sitemap1.xml.gz")?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap1.xml.gz</loc>"#,
r#"</sitemap>"#,
r#"</sitemapindex>"#
)
);
The following example is a sitemap that uses all the optional tags.
use sitemap_xml_writer::{SitemapIndexWriter, Sitemap};
use std::io::Cursor;
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?
.lastmod("2004-10-01T18:23:17+00:00")?
)?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap2.xml.gz")?
.lastmod("2005-01-01")?,
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap1.xml.gz</loc>"#,
r#"<lastmod>2004-10-01T18:23:17+00:00</lastmod>"#,
r#"</sitemap>"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap2.xml.gz</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"</sitemap>"#,
r#"</sitemapindex>"#
)
);
The following example using chrono
crate types.
ⓘ
use sitemap_xml_writer::{SitemapIndexWriter, Sitemap};
use std::io::Cursor;
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?
// `::chrono::DateTime` support
.lastmod(::chrono::DateTime::parse_from_rfc3339("2004-10-01T18:23:17+00:00")?)?
)?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap2.xml.gz")?
// `::chrono::NaiveDate` support
.lastmod(::chrono::NaiveDate::parse_from_str("2005-01-01", "%Y-%m-%d")?)?,
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap1.xml.gz</loc>"#,
r#"<lastmod>2004-10-01T18:23:17+00:00</lastmod>"#,
r#"</sitemap>"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap2.xml.gz</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"</sitemap>"#,
r#"</sitemapindex>"#
)
);
The following example using time
crate types.
ⓘ
use sitemap_xml_writer::{SitemapIndexWriter, Sitemap};
use std::io::Cursor;
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?
// `::time::OffsetDateTime` support
.lastmod(::time::macros::datetime!(2004-10-01 18:23:17+00:00))?
)?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap2.xml.gz")?
// `::time::Date` support
.lastmod(::time::macros::date!(2005-01-01))?,
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap1.xml.gz</loc>"#,
r#"<lastmod>2004-10-01T18:23:17.000000000Z</lastmod>"#,
r#"</sitemap>"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap2.xml.gz</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"</sitemap>"#,
r#"</sitemapindex>"#
)
);
The following example using url
crate types.
ⓘ
use sitemap_xml_writer::{SitemapIndexWriter, Sitemap};
use std::io::Cursor;
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
// <https://crates.io/crates/url> support
// If you want to ensure that the URL is Valid, use `::url::Url`.
// If you use &str, the URL is assumed to be valid and only the length
// check and XML entity escaping are performed.
Sitemap::loc(::url::Url::parse("http://www.example.com/sitemap1.xml.gz")?)?
.lastmod("2004-10-01T18:23:17+00:00")?
)?;
writer.write(
Sitemap::loc(::url::Url::parse("http://www.example.com/sitemap2.xml.gz")?)?
.lastmod("2005-01-01")?,
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap1.xml.gz</loc>"#,
r#"<lastmod>2004-10-01T18:23:17+00:00</lastmod>"#,
r#"</sitemap>"#,
r#"<sitemap>"#,
r#"<loc>http://www.example.com/sitemap2.xml.gz</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"</sitemap>"#,
r#"</sitemapindex>"#
)
);
Implementations§
source§impl<W: Write> SitemapIndexWriter<W>
impl<W: Write> SitemapIndexWriter<W>
sourcepub fn start(inner: W) -> Result<Self, Error>
pub fn start(inner: W) -> Result<Self, Error>
Creates a new SitemapIndexWriter<W>
. At the same time, write the XML declaration and an opening <sitemapindex>
tag.
sourcepub fn start_with_indent(inner: W) -> Result<Self, Error>
pub fn start_with_indent(inner: W) -> Result<Self, Error>
Creates a new SitemapIndexWriter<W>
with indentation enabled. At the same time, write the XML declaration and an opening <sitemapindex>
tag.
sourcepub fn write<'a, S>(&mut self, sitemap: S) -> Result<(), Error>where
S: SealedTryIntoSitemap<'a>,
pub fn write<'a, S>(&mut self, sitemap: S) -> Result<(), Error>where S: SealedTryIntoSitemap<'a>,
Writes a sitemap
element.
sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Unwraps this SitemapIndexWrite<W>
, returning the underlying writer.