sitemap-xml-writer

Crates.iositemap-xml-writer
lib.rssitemap-xml-writer
version0.1.0
sourcesrc
created_at2023-03-03 13:18:58.677849
updated_at2023-03-03 13:18:58.677849
descriptionA Rust library for writing sitemap.xml
homepage
repositoryhttps://github.com/bouzuya/sitemap-xml-writer
max_upload_size
id799747
size79,214
bouzuya (bouzuya)

documentation

README

sitemaps

The sitemap-xml-writer crate provides writers for sitemap.xml.

crates.io docs.rs

Feature flags

  • "chrono" ... chrono::NaiveDate and chrono::DateTime support
  • "time" ... time::Date and time::OffsetDateTime support
  • "url" ... url::Url support

Usage

Writing sitemap file

use sitemap_xml_writer::{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()?;

let actual = String::from_utf8(writer.into_inner().into_inner())?;
let expected = 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>"#
);
assert_eq!(actual, expected);

Writing sitemap index file

use sitemap_xml_writer::{SitemapIndexWriter};
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.end()?;

let actual = String::from_utf8(writer.into_inner().into_inner())?;
let expected = 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#"</sitemapindex>"#
);
assert_eq!(actual, expected);
Commit count: 13

cargo fmt