use std::default::Default; use errors::*; use feed::{Attachment, Author, Feed}; use item::{Content, Item}; /// Feed Builder /// /// This is used to programmatically build up a Feed object, /// which can be serialized later into a JSON string pub struct Builder(Feed); impl Builder { pub fn new() -> Builder { Builder(Feed::default()) } pub fn title>(mut self, t: I) -> Builder { self.0.title = t.into(); self } pub fn home_page_url>(mut self, url: I) -> Builder { self.0.home_page_url = Some(url.into()); self } pub fn feed_url>(mut self, url: I) -> Builder { self.0.feed_url = Some(url.into()); self } pub fn description>(mut self, desc: I) -> Builder { self.0.description = Some(desc.into()); self } pub fn user_comment>(mut self, cmt: I) -> Builder { self.0.user_comment = Some(cmt.into()); self } pub fn next_url>(mut self, url: I) -> Builder { self.0.next_url = Some(url.into()); self } pub fn icon>(mut self, url: I) -> Builder { self.0.icon = Some(url.into()); self } pub fn favicon>(mut self, url: I) -> Builder { self.0.favicon = Some(url.into()); self } pub fn author(mut self, author: Author) -> Builder { self.0.author = Some(author); self } pub fn expired(mut self) -> Builder { self.0.expired = Some(true); self } pub fn item(mut self, item: Item) -> Builder { self.0.items.push(item); self } pub fn build(self) -> Feed { self.0 } } /// Builder object for an item in a feed pub struct ItemBuilder { pub id: Option, pub url: Option, pub external_url: Option, pub title: Option, pub content: Option, pub summary: Option, pub image: Option, pub banner_image: Option, pub date_published: Option, pub date_modified: Option, pub author: Option, pub tags: Option>, pub attachments: Option>, } impl ItemBuilder { pub fn new() -> ItemBuilder { ItemBuilder { id: None, url: None, external_url: None, title: None, content: None, summary: None, image: None, banner_image: None, date_published: None, date_modified: None, author: None, tags: None, attachments: None, } } pub fn title>(mut self, i: I) -> ItemBuilder { self.title = Some(i.into()); self } pub fn image>(mut self, i: I) -> ItemBuilder { self.image = Some(i.into()); self } pub fn id>(mut self, i: I) -> ItemBuilder { self.id = Some(i.into()); self } pub fn url>(mut self, i: I) -> ItemBuilder { self.url = Some(i.into()); self } pub fn external_url>(mut self, i: I) -> ItemBuilder { self.external_url = Some(i.into()); self } pub fn date_modified>(mut self, i: I) -> ItemBuilder { self.date_modified = Some(i.into()); self } pub fn date_published>(mut self, i: I) -> ItemBuilder { self.date_published = Some(i.into()); self } pub fn tags(mut self, tags: Vec) -> ItemBuilder { self.tags = Some(tags); self } pub fn author(mut self, who: Author) -> ItemBuilder { self.author = Some(who); self } pub fn content_html>(mut self, i: I) -> ItemBuilder { match self.content { Some(Content::Text(t)) => { self.content = Some(Content::Both(i.into(), t)); } _ => { self.content = Some(Content::Html(i.into())); } } self } pub fn content_text>(mut self, i: I) -> ItemBuilder { match self.content { Some(Content::Html(s)) => { self.content = Some(Content::Both(s, i.into())); } _ => { self.content = Some(Content::Text(i.into())); } } self } pub fn build(self) -> Result { if self.id.is_none() || self.content.is_none() { return Err("missing field 'id' or 'content_*'".into()); } Ok(Item { id: self.id.unwrap(), url: self.url, external_url: self.external_url, title: self.title, content: self.content.unwrap(), summary: self.summary, image: self.image, banner_image: self.banner_image, date_published: self.date_published, date_modified: self.date_modified, author: self.author, tags: self.tags, attachments: self.attachments, }) } }