Struct sitemap_xml_writer::SitemapWriter
source · pub struct SitemapWriter<W: Write> { /* private fields */ }
Expand description
A writer for sitemap file.
Examples
The following example is a sitemap containing only one URL specified by &str
.
use sitemap_xml_writer::{Changefreq, Lastmod, Loc, Priority, SitemapWriter, Url};
use std::io::Cursor;
let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
writer.write("http://www.example.com/")?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<url>"#,
r#"<loc>http://www.example.com/</loc>"#,
r#"</url>"#,
r#"</urlset>"#
)
);
The following example is a sitemap that uses all the optional tags. It also includes an example using non-string types.
use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
use std::io::Cursor;
let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Url::loc("http://www.example.com/")?
.lastmod("2005-01-01")?
.changefreq("monthly")?
.priority("0.8")?,
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<url>"#,
r#"<loc>http://www.example.com/</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"<changefreq>monthly</changefreq>"#,
r#"<priority>0.8</priority>"#,
r#"</url>"#,
r#"</urlset>"#
)
);
The following an example using chrono
crate types.
ⓘ
use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
use std::io::Cursor;
let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Url::loc("http://www.example.com/")?
// `::chrono::NaiveDate` and `::chrono::DateTime` are supported.
.lastmod(::chrono::NaiveDate::parse_from_str("2005-01-01", "%Y-%m-%d")?)?
.changefreq(Changefreq::Monthly)?
.priority(0.8)?
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<url>"#,
r#"<loc>http://www.example.com/</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"<changefreq>monthly</changefreq>"#,
r#"<priority>0.8</priority>"#,
r#"</url>"#,
r#"</urlset>"#
)
);
The following an example using time
crate types.
ⓘ
use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
use std::io::Cursor;
let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Url::loc("http://www.example.com/")?
// `::time::Date` and `::time::OffsetDateTime` are supported.
.lastmod(::time::macros::date!(2005-01-01))?
.changefreq(Changefreq::Monthly)?
.priority(0.8)?
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<url>"#,
r#"<loc>http://www.example.com/</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"<changefreq>monthly</changefreq>"#,
r#"<priority>0.8</priority>"#,
r#"</url>"#,
r#"</urlset>"#
)
);
The following an example using url
crate types.
ⓘ
use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
use std::io::Cursor;
let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
writer.write(
// <https://crates.io/crates/url> support
// You can specify `::url::Url`.
// 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.
Url::loc(::url::Url::parse("http://www.example.com/")?)?
.lastmod("2005-01-01")?
.changefreq(Changefreq::Monthly)?
.priority(0.8)?
)?;
writer.end()?;
assert_eq!(
String::from_utf8(writer.into_inner().into_inner())?,
concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<url>"#,
r#"<loc>http://www.example.com/</loc>"#,
r#"<lastmod>2005-01-01</lastmod>"#,
r#"<changefreq>monthly</changefreq>"#,
r#"<priority>0.8</priority>"#,
r#"</url>"#,
r#"</urlset>"#
)
);
Implementations§
source§impl<W: Write> SitemapWriter<W>
impl<W: Write> SitemapWriter<W>
sourcepub fn start(inner: W) -> Result<Self, Error>
pub fn start(inner: W) -> Result<Self, Error>
Creates a new SitemapWriter<W>
. At the same time, write the XML declaration and an opening <urlset>
tag.
sourcepub fn start_with_indent(inner: W) -> Result<Self, Error>
pub fn start_with_indent(inner: W) -> Result<Self, Error>
Creates a new SitemapWriter<W>
with indentation enabled. At the same time, write the XML declaration and an opening <urlset>
tag.
sourcepub fn write<'a, U>(&mut self, url: U) -> Result<(), Error>where
U: SealedTryIntoUrl<'a>,
pub fn write<'a, U>(&mut self, url: U) -> Result<(), Error>where U: SealedTryIntoUrl<'a>,
Writes a url
element.
sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Unwraps this SitemapWrite<W>
, returning the underlying writer.