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>

source

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.

source

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.

source

pub fn write<'a, U>(&mut self, url: U) -> Result<(), Error>where U: SealedTryIntoUrl<'a>,

Writes a url element.

source

pub fn end(&mut self) -> Result<(), Error>

Writes a closing </urlset> tag.

source

pub fn into_inner(self) -> W

Unwraps this SitemapWrite<W>, returning the underlying writer.

Auto Trait Implementations§

§

impl<W> RefUnwindSafe for SitemapWriter<W>where W: RefUnwindSafe,

§

impl<W> Send for SitemapWriter<W>where W: Send,

§

impl<W> Sync for SitemapWriter<W>where W: Sync,

§

impl<W> Unpin for SitemapWriter<W>where W: Unpin,

§

impl<W> UnwindSafe for SitemapWriter<W>where W: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.