rss/extension/itunes/
itunes_item_extension.rs

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
// This file is part of rss.
//
// Copyright © 2015-2021 The rust-syndication Developers
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the MIT License and/or Apache 2.0 License.

use std::collections::BTreeMap;
use std::io::Write;

use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::Error as XmlError;
use quick_xml::Writer;

use super::{parse_image, NAMESPACE};
use crate::extension::util::remove_extension_value;
use crate::extension::Extension;
use crate::toxml::{ToXml, WriterExt};

/// An iTunes item element extension.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "builders", derive(Builder))]
#[cfg_attr(
    feature = "builders",
    builder(
        setter(into),
        default,
        build_fn(name = "build_impl", private, error = "never::Never")
    )
)]
pub struct ITunesItemExtension {
    /// The author of the podcast episode.
    pub author: Option<String>,
    /// Specifies if the podcast episode should be prevented from appearing in the iTunes Store. A
    /// value of `Yes` indicates that the episode should not show up in the iTunes Store. All other
    /// values are ignored.
    pub block: Option<String>,
    /// The artwork for the podcast episode.
    pub image: Option<String>,
    /// The podcast episode duration in one of the following formats: HH:MM:SS, H:MM:SS, MM:SS,
    /// M:SS.
    pub duration: Option<String>,
    /// Specifies whether the podcast episode contains explicit content. A value of `Yes`,
    /// `Explicit`, or `True` indicates that the episode contains explicit content. A value of
    /// `Clean`, `No`, `False` indicates that episode does not contain explicit content.
    pub explicit: Option<String>,
    /// Specifies whether the podcast episode contains embedded closed captioning. A value of `Yes`
    /// indicates that it does. Any other value indicates that it does not.
    pub closed_captioned: Option<String>,
    /// A value used to override the default sorting order for episodes.
    pub order: Option<String>,
    /// A description of the podcast episode.
    pub subtitle: Option<String>,
    /// A summary of the podcast episode.
    pub summary: Option<String>,
    /// Keywords for the podcast. The string contains a comma separated list of keywords.
    pub keywords: Option<String>,
    /// Episode number for this episode.
    pub episode: Option<String>,
    /// Season number for this episode.
    pub season: Option<String>,
    /// Type of episode. Usually `full`, but potentially also `trailer` or `bonus`
    pub episode_type: Option<String>,
}

impl ITunesItemExtension {
    /// Return the author of this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_author("John Doe".to_string());
    /// assert_eq!(extension.author(), Some("John Doe"));
    /// ```
    pub fn author(&self) -> Option<&str> {
        self.author.as_deref()
    }

    /// Set the author of this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_author("John Doe".to_string());
    /// ```
    pub fn set_author<V>(&mut self, author: V)
    where
        V: Into<Option<String>>,
    {
        self.author = author.into();
    }

    /// Return whether this podcast episode should be blocked from appearing in the iTunes Store.
    ///
    /// A value of `Yes` indicates that the podcast should not show up in the iTunes Store. All
    /// other values are ignored.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_block("Yes".to_string());
    /// assert_eq!(extension.block(), Some("Yes"));
    /// ```
    pub fn block(&self) -> Option<&str> {
        self.block.as_deref()
    }

    /// Set whether this podcast episode should be blocked from appearing in the iTunes Store.
    ///
    /// A value of `Yes` indicates that the podcast should not show up in the iTunes Store. All
    /// other values are ignored.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_block("Yes".to_string());
    /// ```
    pub fn set_block<V>(&mut self, block: V)
    where
        V: Into<Option<String>>,
    {
        self.block = block.into();
    }

    /// Return the artwork URL for this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_image("http://example.com/artwork.png".to_string());
    /// assert_eq!(extension.image(), Some("http://example.com/artwork.png"));
    /// ```
    pub fn image(&self) -> Option<&str> {
        self.image.as_deref()
    }

    /// Set the artwork URL for this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_image("http://example.com/artwork.png".to_string());
    /// ```
    pub fn set_image<V>(&mut self, image: V)
    where
        V: Into<Option<String>>,
    {
        self.image = image.into();
    }

    /// Return the duration of this podcast episode.
    ///
    /// The duration should be in one of the following formats: HH:MM:SS, H:MM:SS, MM:SS, M:SS.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_duration("1:00".to_string());
    /// assert_eq!(extension.duration(), Some("1:00"));
    /// ```
    pub fn duration(&self) -> Option<&str> {
        self.duration.as_deref()
    }

    /// Set the duration of this podcast episode.
    ///
    /// The duration should be in one of the following formats: HH:MM:SS, H:MM:SS, MM:SS, M:SS.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_duration("1:00".to_string());
    /// ```
    pub fn set_duration<V>(&mut self, duration: V)
    where
        V: Into<Option<String>>,
    {
        self.duration = duration.into();
    }

    /// Return whether this podcast episode contains explicit content.
    ///
    /// A value of `Yes`, `Explicit`, or `True` indicates that the episode contains explicit
    /// content. A value of `Clean`, `No`, `False` indicates that episode does not contain
    /// explicit content.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_explicit("Yes".to_string());
    /// assert_eq!(extension.explicit(), Some("Yes"));
    /// ```
    pub fn explicit(&self) -> Option<&str> {
        self.explicit.as_deref()
    }

    /// Set whether this podcast episode contains explicit content.
    ///
    /// A value of `Yes`, `Explicit`, or `True` indicates that the episode contains explicit
    /// content. A value of `Clean`, `No`, `False` indicates that episode does not contain
    /// explicit content.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_explicit("Yes".to_string());
    /// ```
    pub fn set_explicit<V>(&mut self, explicit: V)
    where
        V: Into<Option<String>>,
    {
        self.explicit = explicit.into();
    }

    /// Return whether this podcast episode contains embedded closed captioning.
    ///
    /// A value of `Yes` indicates that it does. Any other value indicates that it does not.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_closed_captioned("Yes".to_string());
    /// assert_eq!(extension.closed_captioned(), Some("Yes"));
    /// ```
    pub fn closed_captioned(&self) -> Option<&str> {
        self.closed_captioned.as_deref()
    }

    /// Set whether this podcast episode contains embedded closed captioning.
    ///
    /// A value of `Yes` indicates that it does. Any other value indicates that it does not.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_closed_captioned("Yes".to_string());
    /// ```
    pub fn set_closed_captioned<V>(&mut self, closed_captioned: V)
    where
        V: Into<Option<String>>,
    {
        self.closed_captioned = closed_captioned.into();
    }

    /// Return the value used to override the default sorting order for episodes.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_order("1".to_string());
    /// assert_eq!(extension.order(), Some("1"));
    /// ```
    pub fn order(&self) -> Option<&str> {
        self.order.as_deref()
    }

    /// Set the value used to override the default sorting order for episodes.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_order("1".to_string());
    /// ```
    pub fn set_order<V>(&mut self, order: V)
    where
        V: Into<Option<String>>,
    {
        self.order = order.into();
    }

    /// Return the description of this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_subtitle("An episode".to_string());
    /// assert_eq!(extension.subtitle(), Some("An episode"));
    /// ```
    pub fn subtitle(&self) -> Option<&str> {
        self.subtitle.as_deref()
    }

    /// Set the description of this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_subtitle("An episode".to_string());
    /// ```
    pub fn set_subtitle<V>(&mut self, subtitle: V)
    where
        V: Into<Option<String>>,
    {
        self.subtitle = subtitle.into();
    }

    /// Return the summary for this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_summary("An episode".to_string());
    /// assert_eq!(extension.summary(), Some("An episode"));
    /// ```
    pub fn summary(&self) -> Option<&str> {
        self.summary.as_deref()
    }

    /// Set the summary for this podcast episode.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_summary("An episode".to_string());
    /// ```
    pub fn set_summary<V>(&mut self, summary: V)
    where
        V: Into<Option<String>>,
    {
        self.summary = summary.into();
    }

    /// Return the keywords for this podcast episode.
    ///
    /// A comma separated list of keywords.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_keywords("technology".to_string());
    /// assert_eq!(extension.keywords(), Some("technology"));
    /// ```
    pub fn keywords(&self) -> Option<&str> {
        self.keywords.as_deref()
    }

    /// Set the keywords for this podcast episode.
    ///
    /// A comma separated list of keywords.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_keywords("technology".to_string());
    /// ```
    pub fn set_keywords<V>(&mut self, keywords: V)
    where
        V: Into<Option<String>>,
    {
        self.keywords = keywords.into();
    }

    /// Return the episode number of this podcast episode
    ///
    /// The episode number will be a string although it is typically a number in practice
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_episode("3".to_string());
    /// assert_eq!(extension.episode(), Some("3"));
    /// ```
    pub fn episode(&self) -> Option<&str> {
        self.episode.as_deref()
    }

    /// Set the the episode number for this episode.
    ///
    /// An string.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_episode("2".to_string());
    /// assert_eq!(extension.episode(), Some("2"));
    /// ```
    pub fn set_episode<V>(&mut self, episode: V)
    where
        V: Into<Option<String>>,
    {
        self.episode = episode.into()
    }

    /// Return the season of this podcast episode
    ///
    /// The season will be a string although it is typically a number in practice
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_season("3".to_string());
    /// assert_eq!(extension.season(), Some("3"));
    /// ```
    pub fn season(&self) -> Option<&str> {
        self.season.as_deref()
    }

    /// Set the the season number for this episode.
    ///
    /// An integer.
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_season("3".to_string());
    /// assert_eq!(extension.season(), Some("3"));
    /// ```
    pub fn set_season<V>(&mut self, season: V)
    where
        V: Into<Option<String>>,
    {
        self.season = season.into()
    }

    /// Return the episode_type of this podcast episode
    ///
    /// The episode type will be a string usually "full" "trailer" or "bonus"
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_episode_type("trailer".to_string());
    /// assert_eq!(extension.episode_type(), Some("trailer"));
    /// ```
    pub fn episode_type(&self) -> Option<&str> {
        self.episode_type.as_deref()
    }

    /// Set the the episode type for this episode.
    ///
    /// A string, usually "full" but maybe "trailer" or "bonus"
    ///
    /// # Examples
    ///
    /// ```
    /// use rss::extension::itunes::ITunesItemExtension;
    ///
    /// let mut extension = ITunesItemExtension::default();
    /// extension.set_episode_type("full".to_string());
    /// assert_eq!(extension.episode_type(), Some("full"));
    /// ```
    pub fn set_episode_type<V>(&mut self, episode_type: V)
    where
        V: Into<Option<String>>,
    {
        self.episode_type = episode_type.into()
    }
}

impl ITunesItemExtension {
    /// Create an `ITunesChannelExtension` from a `BTreeMap`.
    pub fn from_map(mut map: BTreeMap<String, Vec<Extension>>) -> Self {
        Self {
            author: remove_extension_value(&mut map, "author"),
            block: remove_extension_value(&mut map, "block"),
            image: parse_image(&mut map),
            duration: remove_extension_value(&mut map, "duration"),
            explicit: remove_extension_value(&mut map, "explicit"),
            closed_captioned: remove_extension_value(&mut map, "isClosedCaptioned"),
            order: remove_extension_value(&mut map, "order"),
            subtitle: remove_extension_value(&mut map, "subtitle"),
            summary: remove_extension_value(&mut map, "summary"),
            keywords: remove_extension_value(&mut map, "keywords"),
            episode: remove_extension_value(&mut map, "episode"),
            season: remove_extension_value(&mut map, "season"),
            episode_type: remove_extension_value(&mut map, "episodeType"),
        }
    }
}

impl ToXml for ITunesItemExtension {
    fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> {
        if let Some(author) = self.author.as_ref() {
            writer.write_text_element("itunes:author", author)?;
        }

        if let Some(block) = self.block.as_ref() {
            writer.write_text_element("itunes:block", block)?;
        }

        if let Some(image) = self.image.as_ref() {
            let name = "itunes:image";
            let mut element = BytesStart::new(name);
            element.push_attribute(("href", &**image));
            writer.write_event(Event::Start(element))?;
            writer.write_event(Event::End(BytesEnd::new(name)))?;
        }

        if let Some(duration) = self.duration.as_ref() {
            writer.write_text_element("itunes:duration", duration)?;
        }

        if let Some(explicit) = self.explicit.as_ref() {
            writer.write_text_element("itunes:explicit", explicit)?;
        }

        if let Some(closed_captioned) = self.closed_captioned.as_ref() {
            writer.write_text_element("itunes:isClosedCaptioned", closed_captioned)?;
        }

        if let Some(order) = self.order.as_ref() {
            writer.write_text_element("itunes:order", order)?;
        }

        if let Some(subtitle) = self.subtitle.as_ref() {
            writer.write_text_element("itunes:subtitle", subtitle)?;
        }

        if let Some(summary) = self.summary.as_ref() {
            writer.write_text_element("itunes:summary", summary)?;
        }

        if let Some(keywords) = self.keywords.as_ref() {
            writer.write_text_element("itunes:keywords", keywords)?;
        }

        if let Some(episode) = self.episode.as_ref() {
            writer.write_text_element("itunes:episode", episode)?;
        }

        if let Some(season) = self.season.as_ref() {
            writer.write_text_element("itunes:season", season)?;
        }

        if let Some(episode_type) = self.episode_type.as_ref() {
            writer.write_text_element("itunes:episodeType", episode_type)?;
        }

        Ok(())
    }

    fn used_namespaces(&self) -> BTreeMap<String, String> {
        let mut namespaces = BTreeMap::new();
        namespaces.insert("itunes".to_owned(), NAMESPACE.to_owned());
        namespaces
    }
}

#[cfg(feature = "builders")]
impl ITunesItemExtensionBuilder {
    /// Builds a new `ITunesItemExtension`.
    pub fn build(&self) -> ITunesItemExtension {
        self.build_impl().unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[cfg(feature = "builders")]
    fn test_builder() {
        assert_eq!(
            ITunesItemExtensionBuilder::default()
                .author("John Doe".to_string())
                .build(),
            ITunesItemExtension {
                author: Some("John Doe".to_string()),
                ..Default::default()
            }
        );
    }
}