feed/channel/
item_builder.rs

1// This file is part of feed.
2//
3// Copyright © 2015-2017 Chris Palmer <pennstate5013@gmail.com>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation; either version 3 of the License, or
8// (at your option) any later version.
9
10
11//! The fields can be set for item by using the methods under `ItemBuilder`.
12
13
14use ItemBuilder;
15use rss::{Category, Enclosure, Guid, Item, Source};
16use rss::extension::itunes::ITunesItemExtension;
17use utils::string_utils;
18
19
20impl ItemBuilder
21{
22    /// Construct a new `ItemBuilder` and return default values.
23    ///
24    /// # Examples
25    ///
26    /// ```
27    /// use feed::ItemBuilder;
28    ///
29    /// let item_builder = ItemBuilder::new();
30    /// ```
31    pub fn new() -> ItemBuilder
32    {
33        ItemBuilder::default()
34    }
35
36
37    /// Set the optional title that exists under `Item`.
38    ///
39    /// # Examples
40    ///
41    /// ```
42    /// use feed::ItemBuilder;
43    ///
44    /// let mut item_builder = ItemBuilder::new();
45    /// item_builder.title(Some("Making Music with Linux | LAS
46    /// 408".to_owned()));
47    /// ```
48    pub fn title(&mut self, title: Option<String>) -> &mut ItemBuilder
49    {
50        self.title = title;
51        self
52    }
53
54
55    /// Set the optional link that exists under `Item`.
56    ///
57    /// # Examples
58    ///
59    /// ```
60    /// use feed::ItemBuilder;
61    ///
62    /// let mut item_builder = ItemBuilder::new();
63    /// item_builder.link(Some("http://www.jupiterbroadcasting.com".
64    /// to_owned()));
65    /// ```
66    pub fn link(&mut self, link: Option<String>) -> &mut ItemBuilder
67    {
68        self.link = link;
69        self
70    }
71
72
73    /// Set the optional description that exists under `Item`.
74    ///
75    /// # Examples
76    ///
77    /// ```
78    /// use feed::ItemBuilder;
79    ///
80    /// let mut item_builder = ItemBuilder::new();
81    /// item_builder.description(Some("This is a test description".to_owned()));
82    /// ```
83    pub fn description(&mut self, description: Option<String>) -> &mut ItemBuilder
84    {
85        self.description = description;
86        self
87    }
88
89
90    /// Set the optional author that exists under `Item`.
91    ///
92    /// # Examples
93    ///
94    /// ```
95    /// use feed::ItemBuilder;
96    ///
97    /// let mut item_builder = ItemBuilder::new();
98    /// item_builder.author(Some("Chris Fisher".to_owned()));
99    /// ```
100    pub fn author(&mut self, author: Option<String>) -> &mut ItemBuilder
101    {
102        self.author = author;
103        self
104    }
105
106
107    /// Set the optional categories that exists under `Item`.
108    ///
109    /// # Examples
110    ///
111    /// ```
112    /// use feed::{CategoryBuilder, ItemBuilder};
113    ///
114    /// let category = CategoryBuilder::new()
115    ///     .finalize()
116    ///     .unwrap();;
117    /// let categories = vec![category];
118    ///
119    /// let mut item_builder = ItemBuilder::new();
120    /// item_builder.categories(categories);
121    /// ```
122    pub fn categories(&mut self, categories: Vec<Category>) -> &mut ItemBuilder
123    {
124        self.categories = categories;
125        self
126    }
127
128
129    /// Set the optional comments that exists under `Item`.
130    ///
131    /// # Examples
132    ///
133    /// ```
134    /// use feed::ItemBuilder;
135    ///
136    /// let mut item_builder = ItemBuilder::new();
137    /// item_builder.comments(Some("Test Comment".to_owned()));
138    /// ```
139    pub fn comments(&mut self, comments: Option<String>) -> &mut ItemBuilder
140    {
141        self.comments = comments;
142        self
143    }
144
145
146    /// Set the optional enclosure that exists under `Item`.
147    ///
148    /// # Examples
149    ///
150    /// ```
151    /// use feed::{EnclosureBuilder, ItemBuilder};
152    ///
153    /// let url = "http://www.podtrac.com/pts/redirect.ogg/".to_owned()
154    /// + "traffic.libsyn.com/jnite/linuxactionshowep408.ogg";
155    ///
156    /// let enclosure = EnclosureBuilder::new()
157    ///     .url(url.as_str())
158    ///     .mime_type("audio/ogg")
159    ///     .finalize()
160    ///     .unwrap();
161    ///
162    /// let mut item_builder = ItemBuilder::new();
163    /// item_builder.enclosure(Some(enclosure));
164    /// ```
165    pub fn enclosure(&mut self, enclosure: Option<Enclosure>) -> &mut ItemBuilder
166    {
167        self.enclosure = enclosure;
168        self
169    }
170
171
172    /// Set the optional guid that exists under `Item`.
173    ///
174    /// # Examples
175    ///
176    /// ```
177    /// use feed::{GuidBuilder, ItemBuilder};
178    ///
179    /// let guid = GuidBuilder::new()
180    ///     .finalize()
181    ///     .unwrap();
182    ///
183    /// let mut item_builder = ItemBuilder::new();
184    /// item_builder.guid(Some(guid));
185    /// ```
186    pub fn guid(&mut self, guid: Option<Guid>) -> &mut ItemBuilder
187    {
188        self.guid = guid;
189        self
190    }
191
192
193    /// Set the optional pub date that exists under `Item`.
194    ///
195    /// # Examples
196    ///
197    /// ```
198    /// use feed::ItemBuilder;
199    ///
200    /// let mut item_builder = ItemBuilder::new();
201    /// item_builder.pub_date(Some("Sun, 13 Mar 2016
202    /// 20:02:02-0700".to_owned()));
203    /// ```
204    pub fn pub_date(&mut self, pub_date: Option<String>) -> &mut ItemBuilder
205    {
206        self.pub_date = pub_date;
207        self
208    }
209
210
211    /// Set the optional source that exists under `Item`.
212    ///
213    /// # Examples
214    ///
215    /// ```
216    /// use feed::{ItemBuilder, SourceBuilder};
217    ///
218    /// let url = "http://www.tomalak.org/links2.xml";
219    ///
220    /// let source = SourceBuilder::new()
221    ///     .url(url)
222    ///     .finalize()
223    ///     .unwrap();
224    ///
225    /// let mut item_builder = ItemBuilder::new();
226    /// item_builder.source(Some(source));
227    /// ```
228    pub fn source(&mut self, source: Option<Source>) -> &mut ItemBuilder
229    {
230        self.source = source;
231        self
232    }
233
234
235    /// Set the optional itunes_ext that exists under `Item`.
236    ///
237    /// # Examples
238    ///
239    /// ```
240    /// use feed::ItemBuilder;
241    /// use feed::extension::itunes::ITunesItemExtensionBuilder;
242    ///
243    /// let url = "http://www.tomalak.org/links2.xml";
244    ///
245    /// let itunes_item = ITunesItemExtensionBuilder::new()
246    ///     .author(Some("author".to_owned()))
247    ///     .block(Some("block".to_owned()))
248    ///     .image(Some("image".to_owned()))
249    ///     .duration(Some("duration".to_owned()))
250    ///     .explicit(Some("explicit".to_owned()))
251    ///     .closed_captioned(Some("closed_captioned".to_owned()))
252    ///     .order(Some("order".to_owned()))
253    ///     .subtitle(Some("subtitle".to_owned()))
254    ///     .summary(Some("summary".to_owned()))
255    ///     .keywords(Some("keywords".to_owned()))
256    ///     .finalize()
257    ///     .unwrap();
258    ///
259    /// let mut item_builder = ItemBuilder::new();
260    /// item_builder.itunes_ext(Some(itunes_item));
261    /// ```
262    pub fn itunes_ext(&mut self, itunes_ext: Option<ITunesItemExtension>) -> &mut ItemBuilder
263    {
264        self.itunes_ext = itunes_ext;
265        self
266    }
267
268
269    /// Validate the contents of `Item`.
270    ///
271    /// # Examples
272    ///
273    /// ```
274    /// use feed::ItemBuilder;
275    ///
276    /// let item = ItemBuilder::new()
277    ///     .title(Some("Making Music with Linux | LAS 408".to_owned()))
278    ///     .link(Some("http://www.jupiterbroadcasting.com".to_owned()))
279    ///     .description(None)
280    ///     .author(None)
281    ///     .categories(Vec::new())
282    ///     .comments(None)
283    ///     .enclosure(None)
284    ///     .guid(None)
285    ///     .pub_date(None)
286    ///     .source(None)
287    ///     .validate().unwrap()
288    ///     .finalize().unwrap();
289    /// ```
290    pub fn validate(&mut self) -> Result<&mut ItemBuilder, String>
291    {
292        if self.title.is_none() && self.description.is_none()
293        {
294            return Err("Either Title or Description must have a value.".to_owned());
295        }
296
297        let link = self.link.clone();
298        if link.is_some()
299        {
300            string_utils::str_to_url(link.unwrap().as_str())?;
301        }
302
303        let comments = self.comments.clone();
304        if comments.is_some()
305        {
306            string_utils::str_to_url(comments.unwrap().as_str())?;
307        }
308
309        string_utils::option_string_to_option_date(self.pub_date.clone())?;
310
311        Ok(self)
312    }
313
314
315    /// Construct the `Item` from the `ItemBuilder`.
316    ///
317    /// # Examples
318    ///
319    /// ```
320    /// use feed::ItemBuilder;
321    ///
322    /// let item = ItemBuilder::new()
323    ///     .title(Some("Making Music with Linux | LAS 408".to_owned()))
324    ///     .link(Some("http://www.jupiterbroadcasting.com".to_owned()))
325    ///     .description(None)
326    ///     .author(None)
327    ///     .categories(Vec::new())
328    ///     .comments(None)
329    ///     .enclosure(None)
330    ///     .guid(None)
331    ///     .pub_date(None)
332    ///     .source(None)
333    ///     .finalize()
334    ///     .unwrap();
335    /// ```
336    pub fn finalize(&self) -> Result<Item, String>
337    {
338        Ok(Item {
339               title: self.title.clone(),
340               link: self.link.clone(),
341               description: self.description.clone(),
342               author: self.author.clone(),
343               categories: self.categories.clone(),
344               comments: self.comments.clone(),
345               enclosure: self.enclosure.clone(),
346               guid: self.guid.clone(),
347               pub_date: self.pub_date.clone(),
348               source: self.source.clone(),
349               itunes_ext: self.itunes_ext.clone(),
350               ..Default::default()
351           })
352    }
353}