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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! Type definitions for sitemap entries

/// How frequently the page is likely to change
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChangeFreq {
    Always,
    Hourly,
    Daily,
    Weekly,
    Monthly,
    Yearly,
    Never,
}

impl ChangeFreq {
    pub fn as_str(&self) -> &'static str {
        match self {
            ChangeFreq::Always => "always",
            ChangeFreq::Hourly => "hourly",
            ChangeFreq::Daily => "daily",
            ChangeFreq::Weekly => "weekly",
            ChangeFreq::Monthly => "monthly",
            ChangeFreq::Yearly => "yearly",
            ChangeFreq::Never => "never",
        }
    }
}

impl std::fmt::Display for ChangeFreq {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// A standard URL entry in a sitemap
#[derive(Debug, Clone)]
pub struct UrlEntry {
    /// The URL of the page (required)
    pub loc: String,

    /// The date of last modification (W3C Datetime format)
    pub lastmod: Option<String>,

    /// How frequently the page is likely to change
    pub changefreq: Option<ChangeFreq>,

    /// The priority of this URL relative to other URLs (0.0 to 1.0)
    pub priority: Option<f32>,
}

impl UrlEntry {
    /// Create a new URL entry
    pub fn new(loc: impl Into<String>) -> Self {
        Self {
            loc: loc.into(),
            lastmod: None,
            changefreq: None,
            priority: None,
        }
    }

    /// Set the last modification date (W3C Datetime format: YYYY-MM-DD)
    pub fn lastmod(mut self, lastmod: impl Into<String>) -> Self {
        self.lastmod = Some(lastmod.into());
        self
    }

    /// Set the change frequency
    pub fn changefreq(mut self, changefreq: ChangeFreq) -> Self {
        self.changefreq = Some(changefreq);
        self
    }

    /// Set the priority (0.0 to 1.0)
    pub fn priority(mut self, priority: f32) -> Self {
        self.priority = Some(priority);
        self
    }
}

/// Geographic location for an image
#[derive(Debug, Clone)]
pub struct GeoLocation {
    pub city: Option<String>,
    pub state: Option<String>,
    pub country: Option<String>,
}

impl GeoLocation {
    pub fn new() -> Self {
        Self {
            city: None,
            state: None,
            country: None,
        }
    }

    pub fn city(mut self, city: impl Into<String>) -> Self {
        self.city = Some(city.into());
        self
    }

    pub fn state(mut self, state: impl Into<String>) -> Self {
        self.state = Some(state.into());
        self
    }

    pub fn country(mut self, country: impl Into<String>) -> Self {
        self.country = Some(country.into());
        self
    }
}

/// An image entry in an image sitemap
#[derive(Debug, Clone)]
pub struct ImageEntry {
    /// The URL of the image (required)
    pub loc: String,

    /// A caption for the image
    pub caption: Option<String>,

    /// The geographic location of the image
    pub geo_location: Option<GeoLocation>,

    /// The title of the image
    pub title: Option<String>,

    /// A URL to the license of the image
    pub license: Option<String>,
}

impl ImageEntry {
    pub fn new(loc: impl Into<String>) -> Self {
        Self {
            loc: loc.into(),
            caption: None,
            geo_location: None,
            title: None,
            license: None,
        }
    }

    pub fn caption(mut self, caption: impl Into<String>) -> Self {
        self.caption = Some(caption.into());
        self
    }

    pub fn geo_location(mut self, geo: GeoLocation) -> Self {
        self.geo_location = Some(geo);
        self
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn license(mut self, license: impl Into<String>) -> Self {
        self.license = Some(license.into());
        self
    }
}

/// A URL entry with associated images
#[derive(Debug, Clone)]
pub struct UrlWithImages {
    pub url: UrlEntry,
    pub images: Vec<ImageEntry>,
}

impl UrlWithImages {
    pub fn new(url: UrlEntry) -> Self {
        Self {
            url,
            images: Vec::new(),
        }
    }

    pub fn add_image(mut self, image: ImageEntry) -> Self {
        self.images.push(image);
        self
    }

    pub fn add_images(mut self, images: Vec<ImageEntry>) -> Self {
        self.images.extend(images);
        self
    }
}

/// Video platform restriction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VideoPlatform {
    Web,
    Mobile,
    Tv,
}

impl VideoPlatform {
    pub fn as_str(&self) -> &'static str {
        match self {
            VideoPlatform::Web => "web",
            VideoPlatform::Mobile => "mobile",
            VideoPlatform::Tv => "tv",
        }
    }
}

/// Video platform restriction type
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VideoPlatformRestriction {
    Allow(Vec<VideoPlatform>),
    Deny(Vec<VideoPlatform>),
}

/// Video country restriction
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VideoCountryRestriction {
    Allow(Vec<String>),
    Deny(Vec<String>),
}

/// Video price
#[derive(Debug, Clone)]
pub struct VideoPrice {
    pub currency: String,
    pub amount: f32,
    pub resolution: Option<String>,
    pub type_: Option<String>,
}

impl VideoPrice {
    pub fn new(currency: impl Into<String>, amount: f32) -> Self {
        Self {
            currency: currency.into(),
            amount,
            resolution: None,
            type_: None,
        }
    }

    pub fn resolution(mut self, resolution: impl Into<String>) -> Self {
        self.resolution = Some(resolution.into());
        self
    }

    pub fn type_(mut self, type_: impl Into<String>) -> Self {
        self.type_ = Some(type_.into());
        self
    }
}

/// Whether the video requires a subscription
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VideoRequiresSubscription {
    Yes,
    No,
}

impl VideoRequiresSubscription {
    pub fn as_str(&self) -> &'static str {
        match self {
            VideoRequiresSubscription::Yes => "yes",
            VideoRequiresSubscription::No => "no",
        }
    }
}

/// Video uploader information
#[derive(Debug, Clone)]
pub struct VideoUploader {
    pub name: String,
    pub info_url: Option<String>,
}

impl VideoUploader {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            info_url: None,
        }
    }

    pub fn info_url(mut self, url: impl Into<String>) -> Self {
        self.info_url = Some(url.into());
        self
    }
}

/// Whether the video is live
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VideoLive {
    Yes,
    No,
}

impl VideoLive {
    pub fn as_str(&self) -> &'static str {
        match self {
            VideoLive::Yes => "yes",
            VideoLive::No => "no",
        }
    }
}

/// A video entry in a video sitemap
#[derive(Debug, Clone)]
pub struct VideoEntry {
    /// URL of the thumbnail image (required)
    pub thumbnail_loc: String,

    /// Title of the video (required, max 100 characters)
    pub title: String,

    /// Description of the video (required, max 2048 characters)
    pub description: String,

    /// URL of the video content
    pub content_loc: Option<String>,

    /// URL of the video player
    pub player_loc: Option<String>,

    /// Duration of the video in seconds (0-28800)
    pub duration: Option<u32>,

    /// Publication date
    pub publication_date: Option<String>,

    /// Expiration date
    pub expiration_date: Option<String>,

    /// Rating of the video (0.0 to 5.0)
    pub rating: Option<f32>,

    /// Number of views
    pub view_count: Option<u64>,

    /// Whether the video is family friendly
    pub family_friendly: Option<bool>,

    /// Tags associated with the video
    pub tags: Vec<String>,

    /// Video category
    pub category: Option<String>,

    /// Country restrictions
    pub restriction: Option<VideoCountryRestriction>,

    /// Gallery title
    pub gallery_loc: Option<String>,

    /// Platform restrictions
    pub platform: Option<VideoPlatformRestriction>,

    /// Prices
    pub prices: Vec<VideoPrice>,

    /// Requires subscription
    pub requires_subscription: Option<VideoRequiresSubscription>,

    /// Uploader information
    pub uploader: Option<VideoUploader>,

    /// Whether the video is live
    pub live: Option<VideoLive>,
}

impl VideoEntry {
    pub fn new(
        thumbnail_loc: impl Into<String>,
        title: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            thumbnail_loc: thumbnail_loc.into(),
            title: title.into(),
            description: description.into(),
            content_loc: None,
            player_loc: None,
            duration: None,
            publication_date: None,
            expiration_date: None,
            rating: None,
            view_count: None,
            family_friendly: None,
            tags: Vec::new(),
            category: None,
            restriction: None,
            gallery_loc: None,
            platform: None,
            prices: Vec::new(),
            requires_subscription: None,
            uploader: None,
            live: None,
        }
    }

    pub fn content_loc(mut self, loc: impl Into<String>) -> Self {
        self.content_loc = Some(loc.into());
        self
    }

    pub fn player_loc(mut self, loc: impl Into<String>) -> Self {
        self.player_loc = Some(loc.into());
        self
    }

    pub fn duration(mut self, duration: u32) -> Self {
        self.duration = Some(duration);
        self
    }

    pub fn publication_date(mut self, date: impl Into<String>) -> Self {
        self.publication_date = Some(date.into());
        self
    }

    pub fn expiration_date(mut self, date: impl Into<String>) -> Self {
        self.expiration_date = Some(date.into());
        self
    }

    pub fn rating(mut self, rating: f32) -> Self {
        self.rating = Some(rating);
        self
    }

    pub fn view_count(mut self, count: u64) -> Self {
        self.view_count = Some(count);
        self
    }

    pub fn family_friendly(mut self, family_friendly: bool) -> Self {
        self.family_friendly = Some(family_friendly);
        self
    }

    pub fn add_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    pub fn add_tags(mut self, tags: Vec<String>) -> Self {
        self.tags.extend(tags);
        self
    }

    pub fn category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }

    pub fn restriction(mut self, restriction: VideoCountryRestriction) -> Self {
        self.restriction = Some(restriction);
        self
    }

    pub fn gallery_loc(mut self, loc: impl Into<String>) -> Self {
        self.gallery_loc = Some(loc.into());
        self
    }

    pub fn platform(mut self, platform: VideoPlatformRestriction) -> Self {
        self.platform = Some(platform);
        self
    }

    pub fn add_price(mut self, price: VideoPrice) -> Self {
        self.prices.push(price);
        self
    }

    pub fn requires_subscription(mut self, requires: VideoRequiresSubscription) -> Self {
        self.requires_subscription = Some(requires);
        self
    }

    pub fn uploader(mut self, uploader: VideoUploader) -> Self {
        self.uploader = Some(uploader);
        self
    }

    pub fn live(mut self, live: VideoLive) -> Self {
        self.live = Some(live);
        self
    }
}

/// A URL entry with associated videos
#[derive(Debug, Clone)]
pub struct UrlWithVideos {
    pub url: UrlEntry,
    pub videos: Vec<VideoEntry>,
}

impl UrlWithVideos {
    pub fn new(url: UrlEntry) -> Self {
        Self {
            url,
            videos: Vec::new(),
        }
    }

    pub fn add_video(mut self, video: VideoEntry) -> Self {
        self.videos.push(video);
        self
    }

    pub fn add_videos(mut self, videos: Vec<VideoEntry>) -> Self {
        self.videos.extend(videos);
        self
    }
}

/// Entry for a sitemap index
#[derive(Debug, Clone)]
pub struct SitemapIndexEntry {
    /// URL of the sitemap
    pub loc: String,

    /// Last modification date
    pub lastmod: Option<String>,
}

impl SitemapIndexEntry {
    pub fn new(loc: impl Into<String>) -> Self {
        Self {
            loc: loc.into(),
            lastmod: None,
        }
    }

    pub fn lastmod(mut self, lastmod: impl Into<String>) -> Self {
        self.lastmod = Some(lastmod.into());
        self
    }
}

/// Publication information for a news article
#[derive(Debug, Clone)]
pub struct NewsPublication {
    /// Name of the news publication (required)
    /// Must exactly match the name as it appears on news.google.com
    pub name: String,

    /// Language code (required)
    /// ISO 639 standard (2-3 letters)
    /// Exception: zh-cn for Simplified Chinese, zh-tw for Traditional Chinese
    pub language: String,
}

impl NewsPublication {
    pub fn new(name: impl Into<String>, language: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            language: language.into(),
        }
    }
}

/// A news article entry for Google News sitemap
#[derive(Debug, Clone)]
pub struct NewsEntry {
    /// Publication information (required)
    pub publication: NewsPublication,

    /// Article publication date (required)
    /// W3C format: YYYY-MM-DD or YYYY-MM-DDThh:mm:ssTZD
    pub publication_date: String,

    /// Article headline (required)
    /// Should not include author name, publication name, or date
    pub title: String,

    /// Keywords describing the topic (optional)
    /// Comma-separated list
    pub keywords: Option<String>,

    /// Stock tickers related to the article (optional)
    /// Up to 5 tickers, comma-separated
    pub stock_tickers: Option<String>,
}

impl NewsEntry {
    pub fn new(
        publication: NewsPublication,
        publication_date: impl Into<String>,
        title: impl Into<String>,
    ) -> Self {
        Self {
            publication,
            publication_date: publication_date.into(),
            title: title.into(),
            keywords: None,
            stock_tickers: None,
        }
    }

    pub fn keywords(mut self, keywords: impl Into<String>) -> Self {
        self.keywords = Some(keywords.into());
        self
    }

    pub fn stock_tickers(mut self, tickers: impl Into<String>) -> Self {
        self.stock_tickers = Some(tickers.into());
        self
    }
}

/// A URL entry with associated news article
#[derive(Debug, Clone)]
pub struct UrlWithNews {
    pub url: UrlEntry,
    pub news: NewsEntry,
}

impl UrlWithNews {
    pub fn new(url: UrlEntry, news: NewsEntry) -> Self {
        Self { url, news }
    }
}

/// A URL entry that can combine multiple sitemap extensions
/// Allows combining image, video, and news metadata in a single URL
#[derive(Debug, Clone)]
pub struct UrlWithExtensions {
    pub url: UrlEntry,
    pub images: Vec<ImageEntry>,
    pub videos: Vec<VideoEntry>,
    pub news: Option<NewsEntry>,
}

impl UrlWithExtensions {
    /// Create a new URL with extensions
    pub fn new(url: UrlEntry) -> Self {
        Self {
            url,
            images: Vec::new(),
            videos: Vec::new(),
            news: None,
        }
    }

    /// Add an image to this URL
    pub fn add_image(mut self, image: ImageEntry) -> Self {
        self.images.push(image);
        self
    }

    /// Add multiple images to this URL
    pub fn add_images(mut self, images: Vec<ImageEntry>) -> Self {
        self.images.extend(images);
        self
    }

    /// Add a video to this URL
    pub fn add_video(mut self, video: VideoEntry) -> Self {
        self.videos.push(video);
        self
    }

    /// Add multiple videos to this URL
    pub fn add_videos(mut self, videos: Vec<VideoEntry>) -> Self {
        self.videos.extend(videos);
        self
    }

    /// Set news metadata for this URL
    pub fn set_news(mut self, news: NewsEntry) -> Self {
        self.news = Some(news);
        self
    }

    /// Check if this URL has any extensions
    pub fn has_extensions(&self) -> bool {
        !self.images.is_empty() || !self.videos.is_empty() || self.news.is_some()
    }
}