An open API service indexing awesome lists of open source software.

https://github.com/bouzuya/sitemap-xml-writer

A Rust library for writing sitemap.xml
https://github.com/bouzuya/sitemap-xml-writer

rust sitemap xml

Last synced: 10 months ago
JSON representation

A Rust library for writing sitemap.xml

Awesome Lists containing this project

README

          

# sitemap-xml-writer

A Rust library for writing [`sitemap.xml`](https://www.sitemaps.org/).

[![ci](https://github.com/bouzuya/sitemap-xml-writer/workflows/ci/badge.svg)](https://github.com/bouzuya/sitemap-xml-writer/actions)
[![crates.io](https://img.shields.io/crates/v/sitemap-xml-writer)](https://crates.io/crates/sitemap-xml-writer)
[![docs.rs](https://img.shields.io/docsrs/sitemap-xml-writer)](https://docs.rs/crate/sitemap-xml-writer)
[![license](https://img.shields.io/crates/l/sitemap-xml-writer)](#license)

## Feature flags

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

## Usage

### Writing sitemap file

```rust
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#""#,
r#""#,
r#""#,
r#"http://www.example.com/"#,
r#"2005-01-01"#,
r#"monthly"#,
r#"0.8"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
```

### Writing sitemap index file

```rust
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#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#"2004-10-01T18:23:17+00:00"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
```

## License

Licensed under either of

- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.