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
use std::default::Default;

use item::Item;
use builder::Builder;

const VERSION_1: &'static str = "https://jsonfeed.org/version/1";

/// Represents a single feed
///
/// # Examples
///
/// ```rust
/// // Serialize a feed object to a JSON string
///
/// # extern crate jsonfeed;
/// # use std::default::Default;
/// # use jsonfeed::Feed;
/// # fn main() {
/// let feed: Feed = Feed::default();
/// assert_eq!(
///     jsonfeed::to_string(&feed).unwrap(),
///     "{\"version\":\"https://jsonfeed.org/version/1\",\"title\":\"\",\"items\":[]}"
/// );
/// # }
/// ```
///
/// ```rust
/// // Deserialize a feed objects from a JSON String
///
/// # extern crate jsonfeed;
/// # use jsonfeed::Feed;
/// # fn main() {
/// let json = "{\"version\":\"https://jsonfeed.org/version/1\",\"title\":\"\",\"items\":[]}";
/// let feed: Feed = jsonfeed::from_str(&json).unwrap();
/// assert_eq!(
///     feed,
///     Feed::default()
/// );
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Feed {
    pub version: String,
    pub title: String,
    pub items: Vec<Item>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub home_page_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub feed_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_comment: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub favicon: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<Author>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expired: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hubs: Option<Vec<Hub>>,
}

impl Feed {
    /// Used to construct a Feed object
    pub fn builder() -> Builder {
        Builder::new()
    }
}

impl Default for Feed {
    fn default() -> Feed {
        Feed {
            version: VERSION_1.to_string(),
            title: "".to_string(),
            items: vec![],
            home_page_url: None,
            feed_url: None,
            description: None,
            user_comment: None,
            next_url: None,
            icon: None,
            favicon: None,
            author: None,
            expired: None,
            hubs: None,
        }
    }
}

/// Represents an `attachment` for an item
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Attachment {
    url: String,
    mime_type: String,
    title: Option<String>,
    size_in_bytes: Option<u64>,
    duration_in_seconds: Option<u64>,
}

/// Represents an `author` in both a feed and a feed item
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Author {
    name: Option<String>,
    url: Option<String>,
    avatar: Option<String>,
}

impl Author {
    pub fn new() -> Author {
        Author {
            name: None,
            url: None,
            avatar: None,
        }
    }

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

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

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

/// Represents a `hub` for a feed
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Hub {
    #[serde(rename = "type")]
    type_: String,
    url: String,
}

#[cfg(test)]
mod tests {
    use serde_json;
    use std::default::Default;
    use super::*;

    #[test]
    fn serialize_feed() {
        let feed = Feed {
            version: "https://jsonfeed.org/version/1".to_string(),
            title: "some title".to_string(),
            items: vec![],
            home_page_url: None,
            description: None,
            expired: Some(true),
            ..Default::default()
        };
        assert_eq!(
            serde_json::to_string(&feed).unwrap(),
            r#"{"version":"https://jsonfeed.org/version/1","title":"some title","items":[],"expired":true}"#
        );
    }

    #[test]
    fn deserialize_feed() {
        let json = r#"{"version":"https://jsonfeed.org/version/1","title":"some title","items":[]}"#;
        let feed: Feed = serde_json::from_str(&json).unwrap();
        let expected = Feed {
            version: "https://jsonfeed.org/version/1".to_string(),
             title: "some title".to_string(),
             items: vec![],
             ..Default::default()
        };
        assert_eq!(
            feed,
            expected
        );
    }

    #[test]
    fn serialize_attachment() {
        let attachment = Attachment {
            url: "http://example.com".to_string(),
            mime_type: "application/json".to_string(),
            title: Some("some title".to_string()),
            size_in_bytes: Some(1),
            duration_in_seconds: Some(1),
        };
        assert_eq!(
            serde_json::to_string(&attachment).unwrap(),
            r#"{"url":"http://example.com","mime_type":"application/json","title":"some title","size_in_bytes":1,"duration_in_seconds":1}"#
        );
    }

    #[test]
    fn deserialize_attachment() {
        let json = r#"{"url":"http://example.com","mime_type":"application/json","title":"some title","size_in_bytes":1,"duration_in_seconds":1}"#;
        let attachment: Attachment = serde_json::from_str(&json).unwrap();
        let expected = Attachment {
            url: "http://example.com".to_string(),
            mime_type: "application/json".to_string(),
            title: Some("some title".to_string()),
            size_in_bytes: Some(1),
            duration_in_seconds: Some(1),
        };
        assert_eq!(
            attachment,
            expected
        );
    }

    #[test]
    fn serialize_author() {
        let author = Author {
            name: Some("bob jones".to_string()),
            url: Some("http://example.com".to_string()),
            avatar: Some("http://img.com/blah".to_string()),
        };
        assert_eq!(
            serde_json::to_string(&author).unwrap(),
            r#"{"name":"bob jones","url":"http://example.com","avatar":"http://img.com/blah"}"#
        );
    }

    #[test]
    fn deserialize_author() {
        let json = r#"{"name":"bob jones","url":"http://example.com","avatar":"http://img.com/blah"}"#;
        let author: Author = serde_json::from_str(&json).unwrap();
        let expected = Author {
            name: Some("bob jones".to_string()),
            url: Some("http://example.com".to_string()),
            avatar: Some("http://img.com/blah".to_string()),
        };
        assert_eq!(
            author,
            expected
        );
    }

    #[test]
    fn serialize_hub() {
        let hub = Hub {
            type_: "some-type".to_string(),
            url: "http://example.com".to_string(),
        };
        assert_eq!(
            serde_json::to_string(&hub).unwrap(),
            r#"{"type":"some-type","url":"http://example.com"}"#
        )
    }

    #[test]
    fn deserialize_hub() {
        let json = r#"{"type":"some-type","url":"http://example.com"}"#;
        let hub: Hub = serde_json::from_str(&json).unwrap();
        let expected = Hub {
            type_: "some-type".to_string(),
            url: "http://example.com".to_string(),
        };
        assert_eq!(
            hub,
            expected
        );
    }

    #[test]
    fn deser_podcast() {
        let json = r#"{
  "version": "https://jsonfeed.org/version/1",
  "title": "Timetable",
  "home_page_url": "http://timetable.manton.org/",
  "items": [
    {
      "id": "http://timetable.manton.org/2017/04/episode-45-launch-week/",
      "url": "http://timetable.manton.org/2017/04/episode-45-launch-week/",
      "title": "Episode 45: Launch week",
      "content_html": "I’m rolling out early access to Micro.blog this week. I talk about how the first 2 days have gone, mistakes with TestFlight, and what to do next.",
      "date_published": "2017-04-26T01:09:45+00:00",
      "attachments": [
        {
          "url": "http://timetable.manton.org/podcast-download/139/episode-45-launch-week.mp3",
          "mime_type": "audio/mpeg",
          "size_in_bytes": 5236920
        }
      ]
    }
  ]
}"#;
        serde_json::from_str::<Feed>(&json).expect("Failed to deserialize podcast feed");
    }
}