sitemap_generator 0.1.1

A high-performance Rust library for generating XML sitemaps (standard, image, video, and sitemap index)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# Sitemap Generator

[![Crates.io](https://img.shields.io/crates/v/sitemap_generator.svg)](https://crates.io/crates/sitemap_generator)
[![Documentation](https://docs.rs/sitemap_generator/badge.svg)](https://docs.rs/sitemap_generator)
[![MIT License](https://img.shields.io/badge/license-MIT.svg)](LICENSE)

A high-performance Rust library for generating XML sitemaps compliant with the [sitemaps.org protocol 0.9](https://www.sitemaps.org/protocol.html).

## Features

- **Standard XML Sitemaps**: Generate basic sitemaps with URLs, lastmod, changefreq, and priority
- **Image Sitemaps**: Include image metadata (captions, geo-location, titles, licenses)
- **Video Sitemaps**: Include video metadata (thumbnails, descriptions, durations, ratings)
- **News Sitemaps**: Submit news articles to Google News with publication metadata
- **Combined Sitemaps**: Combine multiple extensions (image + video + news) in one sitemap
- **Sitemap Index**: Manage multiple sitemap files for large websites (>50k URLs)
- **Validation**: Automatic validation of URLs, size limits, and protocol compliance
- **Compression**: Built-in gzip compression support (96-98% bandwidth savings)
- **Web Framework Support**: Direct bytes output for Axum, Actix-web, Rocket, etc.
- **Parsing**: Read and parse existing sitemap files
- **Memory Efficient**: ~140 bytes/URL during generation, 0 bytes after (proven)
- **High Performance**: ~830K URLs/second, immediate memory cleanup
- **Optimized Builders**: Pre-allocate capacity with `with_capacity()` for better performance
- **Zero unsafe code**: 100% safe Rust

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
sitemap_generator = "0.1"
```

## Quick Start

### Basic Sitemap

Generate sitemap as String, bytes, or compressed bytes:

```rust
use sitemap_generator::{SitemapBuilder, UrlEntry, ChangeFreq};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Use with_capacity() if you know the number of URLs in advance (recommended)
    let mut builder = SitemapBuilder::with_capacity(1000);

    builder.add_url(UrlEntry::new("https://example.com/")
        .lastmod("2025-11-01")
        .changefreq(ChangeFreq::Daily)
        .priority(1.0));

    builder.add_url(UrlEntry::new("https://example.com/page1")
        .lastmod("2025-11-02")
        .priority(0.8));

    // 1. Generate as String
    let xml = builder.build()?;

    // 2. Generate as bytes (Vec<u8>) - for HTTP responses
    let bytes = builder.build_bytes()?;

    // 3. Generate as compressed bytes (gzip)
    let compressed = builder.build_compressed_bytes()?;

    // 4. Write to file
    builder.write("sitemap.xml")?;

    // 5. Write compressed file
    builder.write_compressed("sitemap.xml.gz")?;

    Ok(())
}
```

### Image Sitemap

```rust
use sitemap_generator::{ImageSitemapBuilder, UrlEntry, UrlWithImages, ImageEntry};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = ImageSitemapBuilder::new();

    let url_with_images = UrlWithImages::new(
        UrlEntry::new("https://example.com/gallery")
            .lastmod("2025-11-01")
    )
    .add_image(
        ImageEntry::new("https://example.com/images/photo1.jpg")
            .title("Beautiful Sunset")
            .caption("A stunning sunset over the ocean")
    );

    builder.add_url(url_with_images);

    let xml = builder.build()?;
    builder.write("image_sitemap.xml")?;

    Ok(())
}
```

### Video Sitemap

```rust
use sitemap_generator::{VideoSitemapBuilder, UrlEntry, UrlWithVideos, VideoEntry};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = VideoSitemapBuilder::new();

    let url_with_video = UrlWithVideos::new(
        UrlEntry::new("https://example.com/videos/intro")
    )
    .add_video(
        VideoEntry::new(
            "https://example.com/thumbnails/intro.jpg",
            "Introduction to Our Product",
            "Learn about our product in this video"
        )
        .content_loc("https://example.com/videos/intro.mp4")
        .duration(300)
        .rating(4.5)
        .family_friendly(true)
        .add_tag("tutorial")
    );

    builder.add_url(url_with_video);

    let xml = builder.build()?;
    builder.write("video_sitemap.xml")?;

    Ok(())
}
```

### News Sitemap

```rust
use sitemap_generator::{NewsSitemapBuilder, UrlEntry, UrlWithNews, NewsEntry, NewsPublication};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = NewsSitemapBuilder::new();

    let publication = NewsPublication::new("TechDaily", "en");
    let news = NewsEntry::new(
        publication,
        "2025-11-01T10:00:00Z",
        "Revolutionary AI Breakthrough Announced"
    )
    .keywords("AI, technology, machine learning")
    .stock_tickers("GOOGL, MSFT");

    let url = UrlEntry::new("https://example.com/tech/ai-breakthrough");
    builder.add_url(UrlWithNews::new(url, news));

    let xml = builder.build()?;
    builder.write("news_sitemap.xml")?;

    Ok(())
}
```

### Combined Sitemap (Multiple Extensions)

Combine image, video, and news extensions in a single sitemap:

```rust
use sitemap_generator::{
    CombinedSitemapBuilder, UrlEntry, UrlWithExtensions,
    ImageEntry, VideoEntry, NewsEntry, NewsPublication
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = CombinedSitemapBuilder::new();

    // News article with images and video
    let news = NewsEntry::new(
        NewsPublication::new("TechDaily", "en"),
        "2025-11-01T10:00:00Z",
        "AI Breakthrough Announced"
    );

    let image = ImageEntry::new("https://example.com/ai-lab.jpg")
        .title("AI Research Lab");

    let video = VideoEntry::new(
        "https://example.com/thumb.jpg",
        "AI Demo Video",
        "Watch the demonstration"
    )
    .content_loc("https://example.com/video.mp4")
    .duration(300);

    builder.add_url(
        UrlWithExtensions::new(UrlEntry::new("https://example.com/article"))
            .add_image(image)
            .add_video(video)
            .set_news(news)
    );

    let xml = builder.build()?;
    builder.write("combined_sitemap.xml")?;

    Ok(())
}
```

### Sitemap Index

```rust
use sitemap_generator::{SitemapIndexBuilder, SitemapIndexEntry};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = SitemapIndexBuilder::new();

    builder.add_sitemap(
        SitemapIndexEntry::new("https://example.com/sitemap1.xml.gz")
            .lastmod("2025-11-01")
    );

    builder.add_sitemap(
        SitemapIndexEntry::new("https://example.com/sitemap2.xml.gz")
            .lastmod("2025-11-02")
    );

    let xml = builder.build()?;
    builder.write("sitemap_index.xml")?;

    Ok(())
}
```

### Web Framework Integration

Use with Axum, Actix-web, Rocket, or other web frameworks:

#### Axum Example

```rust
use axum::{
    response::{Response, IntoResponse},
    http::{StatusCode, header},
};
use sitemap_generator::{SitemapBuilder, UrlEntry};

async fn sitemap() -> impl IntoResponse {
    let mut builder = SitemapBuilder::new();
    builder.add_url(UrlEntry::new("https://example.com/"));

    match builder.build_bytes() {
        Ok(bytes) => Response::builder()
            .status(StatusCode::OK)
            .header(header::CONTENT_TYPE, "application/xml; charset=utf-8")
            .body(bytes.into())
            .unwrap(),
        Err(_) => Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .body(Vec::new().into())
            .unwrap(),
    }
}

// With compression (recommended):
async fn sitemap_compressed() -> impl IntoResponse {
    let mut builder = SitemapBuilder::new();
    builder.add_url(UrlEntry::new("https://example.com/"));

    match builder.build_compressed_bytes() {
        Ok(bytes) => Response::builder()
            .status(StatusCode::OK)
            .header(header::CONTENT_TYPE, "application/xml")
            .header(header::CONTENT_ENCODING, "gzip")
            .body(bytes.into())
            .unwrap(),
        Err(_) => Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .body(Vec::new().into())
            .unwrap(),
    }
}
```

#### Actix-web Example

```rust
use actix_web::{web, HttpResponse, Result};
use sitemap_generator::{SitemapBuilder, UrlEntry};

async fn sitemap() -> Result<HttpResponse> {
    let mut builder = SitemapBuilder::new();
    builder.add_url(UrlEntry::new("https://example.com/"));

    match builder.build_compressed_bytes() {
        Ok(bytes) => Ok(HttpResponse::Ok()
            .content_type("application/xml")
            .insert_header(("Content-Encoding", "gzip"))
            .body(bytes)),
        Err(_) => Ok(HttpResponse::InternalServerError().finish()),
    }
}
```

See [examples/web_framework_usage.rs](examples/web_framework_usage.rs) for more examples.

### Parsing Sitemaps

```rust
use sitemap_generator::SitemapParser;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse from file
    let entries = SitemapParser::parse_file("sitemap.xml")?;

    // Parse compressed file
    let entries = SitemapParser::parse_compressed("sitemap.xml.gz")?;

    // Parse from string
    let xml = r#"<?xml version="1.0"?>
        <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
            <url>
                <loc>https://example.com/</loc>
                <lastmod>2025-11-01</lastmod>
            </url>
        </urlset>"#;
    let entries = SitemapParser::parse_string(xml)?;

    for entry in entries {
        println!("URL: {}", entry.loc);
    }

    Ok(())
}
```

## Performance

This library is designed for high performance and low memory usage:

- **Fast XML Generation**: Uses `quick-xml` for efficient XML writing (~830K URLs/second)
- **Memory Efficient**: ~140 bytes per URL during generation, 0 bytes after (proven with custom allocator)
- **Optimized Allocation**: Use `with_capacity()` to pre-allocate memory and reduce reallocations
- **Immediate Cleanup**: Memory released **immediately** when builder drops (RAII-based)
- **Zero Memory Leaks**: Empirically proven - 1 byte growth across 100 iterations
- **Streaming Compression**: Gzip compression with 50x+ compression ratios (98% bandwidth saved)

### Performance Tips

**Use `with_capacity()` for better performance:**

```rust
// ❌ Without capacity (Vec grows dynamically)
let mut builder = SitemapBuilder::new();
for i in 0..10_000 {
    builder.add_url(UrlEntry::new(format!("https://example.com/page{}", i)));
}

// ✅ With capacity (Vec pre-allocated, ~2-5% faster)
let mut builder = SitemapBuilder::with_capacity(10_000);
for i in 0..10_000 {
    builder.add_url(UrlEntry::new(format!("https://example.com/page{}", i)));
}
```

**Available for all builders:**
- `SitemapBuilder::with_capacity(10_000)`
- `ImageSitemapBuilder::with_capacity(5_000)`
- `VideoSitemapBuilder::with_capacity(1_000)`
- `NewsSitemapBuilder::with_capacity(1_000)`
- `CombinedSitemapBuilder::with_capacity(500)`
- `SitemapIndexBuilder::with_capacity(50)`

### Benchmarks

| Operation              | 10 URLs  | 100 URLs | 1,000 URLs | 10,000 URLs |
|------------------------|----------|----------|------------|-------------|
| Standard Sitemap       | 12.5 µs  | 120 µs   | 1.2 ms     | 12.2 ms     |
| Build Bytes            | 5.7 µs   | 51 µs    | 519 µs     | 5.2 ms      |
| Build Compressed       | 25 µs    | 180 µs   | 1.8 ms     | 18 ms       |
| Image Sitemap (2 imgs) | 28 µs    | 275 µs   | 2.7 ms     | -           |
| Video Sitemap          | 45 µs    | 450 µs   | 4.5 ms     | -           |

**Compression Ratios**: 31x (100 URLs) to 54x (10,000 URLs) - saves 96-98% bandwidth

**Proven Results**:
- Performance: [Criterion.rs]https://github.com/bheisler/criterion.rs benchmarks
- Memory safety: Custom allocator tracking (see [MEMORY_SAFETY.md]docs/MEMORY_SAFETY.md)

**Documentation**:
- [BENCHMARK_RESULTS.md]BENCHMARK_RESULTS.md - Actual benchmark numbers
- [BENCHMARKS.md]BENCHMARKS.md - Performance analysis
- [MEMORY_SAFETY.md]docs/MEMORY_SAFETY.md - Memory leak proof

Run benchmarks yourself:

```bash
# Performance benchmarks
cargo bench
open target/criterion/report/index.html

# Memory safety demo
cargo run --example memory_demo --release
```

## Validation

The library automatically validates:

- URL format (RFC 3986)
- Maximum 50,000 URLs per standard sitemap (1,000 for news sitemaps)
- Maximum 50MB uncompressed size
- URL length (max 2048 characters)
- Date format (W3C Datetime)
- Priority values (0.0 to 1.0)
- Video duration (max 28,800 seconds)
- Video rating (0.0 to 5.0)
- Video title length (max 100 characters)
- Video description length (max 2048 characters)
- News language codes (ISO 639 format)
- Stock tickers (max 5, comma-separated)

You can disable validation if needed:

```rust
let builder = SitemapBuilder::new().validate(false);

// Or with capacity
let builder = SitemapBuilder::with_capacity(10_000).validate(false);
```

## Sitemap Protocol Compliance

This library follows the [sitemaps.org protocol 0.9](https://www.sitemaps.org/protocol.html) specification:

- Standard sitemap with `<urlset>` and `<url>` elements
- Optional `<lastmod>`, `<changefreq>`, and `<priority>` elements
- Image extension: `xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"`
- Video extension: `xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"`
- News extension: `xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"`
- Sitemap index with `<sitemapindex>` and `<sitemap>` elements

## Examples

See the [examples](examples/) directory for more complete examples:

- [basic_sitemap.rs]examples/basic_sitemap.rs - Standard sitemap generation
- [image_sitemap.rs]examples/image_sitemap.rs - Image sitemap with metadata
- [video_sitemap.rs]examples/video_sitemap.rs - Video sitemap with full metadata
- [news_sitemap.rs]examples/news_sitemap.rs - News sitemap for Google News
- [combined_sitemap.rs]examples/combined_sitemap.rs - Combined sitemap with multiple extensions
- [sitemap_index.rs]examples/sitemap_index.rs - Sitemap index for multiple files
- [web_framework_usage.rs]examples/web_framework_usage.rs - Web framework integration examples
- [memory_demo.rs]examples/memory_demo.rs - Memory tracking and cleanup proof

Run examples with:

```bash
cargo run --example basic_sitemap
cargo run --example image_sitemap
cargo run --example video_sitemap
cargo run --example news_sitemap
cargo run --example combined_sitemap
cargo run --example sitemap_index
cargo run --example web_framework_usage
cargo run --example memory_demo --release
```

## Testing

Run the test suite:

```bash
cargo test
```

## License

- MIT license ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## References

- [Sitemaps.org Protocol]https://www.sitemaps.org/protocol.html
- [Google Image Sitemaps]https://developers.google.com/search/docs/crawling-indexing/sitemaps/image-sitemaps
- [Google Video Sitemaps]https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps
- [Google News Sitemaps]https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap
- [Combining Sitemap Extensions]https://developers.google.com/search/docs/crawling-indexing/sitemaps/combine-sitemap-extensions
- [Sitemap Index Files]https://www.sitemaps.org/protocol.html#index