feed-rs 3.0.0

A feed parser that handles Atom, RSS 2.0, RSS 1.0, RSS 0.x and JSON Feed
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
use std::io::BufRead;

use mediatype::{MediaTypeBuf, names};

use crate::model::LinkTarget::CommentsFeed;
use crate::model::{Category, Content, Entry, Feed, FeedType, Generator, Image, Link, Person, Text};
use crate::parser::util;
use crate::parser::util::if_some_then;
use crate::parser::{ParseErrorKind, ParseFeedError, ParseFeedResult};
use crate::parser::{Parser, common};
use crate::xml::{Element, NS};

#[cfg(test)]
mod tests;

/// Parses an Atom feed into our model
pub(crate) fn parse_feed<R: BufRead>(parser: &Parser, root: Element<R>) -> ParseFeedResult<Feed> {
    let mut feed = Feed::new(FeedType::Atom);

    // Extract top-level info
    feed.language = handle_language_attr(&root);

    for child in root.children() {
        let child = child?;
        match child.ns_and_tag() {
            (NS::Atom, "id") => if_some_then(child.child_as_text(), |id| feed.id = id),

            (NS::Atom, "title") => feed.title = handle_text(child)?,

            (NS::Atom, "updated") => if_some_then(child.child_as_text(), |text| feed.updated = parser.parse_timestamp(&text)),

            (NS::Atom, "author") => if_some_then(handle_person("author", child)?, |person| feed.authors.push(person)),

            (NS::Atom, "link") => if_some_then(handle_link(child), |link| feed.links.push(link)),

            (NS::Atom, "category") => if_some_then(handle_category(child), |category| feed.categories.push(category)),

            (NS::Atom, "contributor") => if_some_then(handle_person("contributor", child)?, |person| feed.contributors.push(person)),

            (NS::Atom, "generator") => feed.generator = handle_generator(child),

            (NS::Atom, "icon") => feed.icon = handle_image(child),

            (NS::Atom, "logo") => feed.logo = handle_image(child),

            (NS::Atom, "rights") => feed.rights = handle_text(child)?,

            (NS::Atom, "subtitle") => feed.description = handle_text(child)?,

            (NS::Atom, "entry") => if_some_then(handle_entry(parser, child)?, |entry| feed.entries.push(entry)),

            // Nothing required for unknown elements
            _ => {}
        }
    }

    Ok(feed)
}

/// Parses an Atom entry into our model
///
/// Note that the entry is wrapped in an empty Feed to keep the API consistent
pub(crate) fn parse_entry<R: BufRead>(parser: &Parser, root: Element<R>) -> ParseFeedResult<Feed> {
    let mut feed = Feed::new(FeedType::Atom);

    if_some_then(handle_entry(parser, root)?, |entry| feed.entries.push(entry));

    Ok(feed)
}

// Handles "xml:base" as an attribute (e.g. in Atom feeds)
fn handle_base_attr<R: BufRead>(element: &Element<R>) -> Option<String> {
    element.attr_value("xml:base")
}

// Handles an Atom <category>
fn handle_category<R: BufRead>(element: Element<R>) -> Option<Category> {
    // Always need a term
    if let Some(term) = element.attr_value("term") {
        let mut category = Category::new(&term);

        for attr in element.attributes {
            match attr.name.as_str() {
                "scheme" => category.scheme = Some(attr.value.clone()),
                "label" => category.label = Some(attr.value.clone()),

                // Nothing required for unknown attributes
                _ => {}
            }
        }

        Some(category)
    } else {
        // A missing category isn't fatal
        None
    }
}

// Handles an Atom <content> element
fn handle_content<R: BufRead>(element: Element<R>) -> ParseFeedResult<Option<Content>> {
    // Extract the content type so we can parse the body
    let content_type = element.attr_value("type");

    if let Some(src) = element.attr_value("src") {
        // > If the "src" attribute is present, the "type" attribute SHOULD be provided and MUST be a MIME media type, rather than "text", "html", or "xhtml".
        let mime = match &content_type {
            Some(ct) => ct.parse::<MediaTypeBuf>().map_err(|_| {
                ParseFeedError::ParseError(ParseErrorKind::UnknownMimeType {
                    position: element.buffer_pos,
                    mime: ct.into(),
                })
            })?,
            None => {
                // According to the spec the content type only SHOULD be provided.
                // Unfortunately `Content` has media type as required. So we treat a missing type as text/html as that is probably the most common. Not to mention that `body` will be `None` so we are just providing a content type for nothing.
                MediaTypeBuf::new(names::TEXT, names::HTML)
            }
        };

        if element.child_as_text().is_some() {
            // > If the "src" attribute is present, atom:content MUST be empty.
            // `ParseFeedError` has no appropriate error type, so use `MissingContent` which is what would have been returned before support for `src` was added.
            return Err(ParseFeedError::ParseError(ParseErrorKind::MissingContent {
                position: element.buffer_pos,
                reference: "non-empty atom:content with src",
            }));
        }

        let content = Content {
            body: None,
            content_type: mime,
            src: Some(Link {
                href: src,
                target: None,
                rel: None,
                media_type: content_type,
                href_lang: None,
                title: None,
                length: None,
            }),
            ..Default::default()
        };

        return Ok(Some(content));
    }

    // from http://www.atomenabled.org/developers/syndication/#contentElement
    match content_type.as_deref() {
        // Should be handled as a text element per "In the most common case, the type attribute is either text, html, xhtml, in which case the content element is defined identically to other text constructs"
        Some("text") | Some("html") | Some("xhtml") | Some("text/html") | None => {
            let buffer_pos = element.buffer_pos;
            handle_text(element)?
                .map(|text| {
                    let mut content = Content::default();
                    content.body = Some(text.content);
                    content.content_type = text.content_type;
                    Some(content)
                })
                // The text is required for a text or HTML element
                .ok_or(ParseFeedError::ParseError(ParseErrorKind::MissingContent {
                    position: buffer_pos,
                    reference: "content.text",
                }))
        }

        // XML per "Otherwise, if the type attribute ends in +xml or /xml, then an xml document of this type is contained inline."
        Some(ct) if ct.ends_with(" +xml") || ct.ends_with("/xml") => {
            let buffer_pos = element.buffer_pos;
            handle_text(element)?
                .map(|body| {
                    let mut content = Content::default();
                    content.body = Some(body.content);
                    content.content_type = MediaTypeBuf::new(names::TEXT, names::XML);
                    Some(content)
                })
                // The XML is required for an XML content element
                .ok_or(ParseFeedError::ParseError(ParseErrorKind::MissingContent {
                    position: buffer_pos,
                    reference: "content.xml",
                }))
        }

        // Escaped text per "Otherwise, if the type attribute starts with text, then an escaped document of this type is contained inline." and
        // also handles base64 encoded document of the indicated mime type per "Otherwise, a base64 encoded document of the indicated media type is contained inline."
        Some(ct) => {
            if let Ok(mime) = ct.parse::<MediaTypeBuf>() {
                element
                    .child_as_text()
                    .map(|body| {
                        let content = Content {
                            body: Some(body),
                            content_type: mime,
                            ..Default::default()
                        };
                        Some(content)
                    })
                    // The text is required for an inline text or base64 element
                    .ok_or(ParseFeedError::ParseError(ParseErrorKind::MissingContent {
                        position: element.buffer_pos,
                        reference: "content.inline",
                    }))
            } else {
                Err(ParseFeedError::ParseError(ParseErrorKind::UnknownMimeType {
                    position: element.buffer_pos,
                    mime: ct.into(),
                }))
            }
        }
    }
}

// Handles an Atom <entry>
fn handle_entry<R: BufRead>(parser: &Parser, element: Element<R>) -> ParseFeedResult<Option<Entry>> {
    // Note we have started a new MediaRSS entry scope
    parser.mediarss.entry_begin();

    // Parse the entry
    let mut entry = Entry::default();
    for child in element.children() {
        let child = child?;
        match child.ns_and_tag() {
            // Extract the fields from the spec
            (NS::Atom, "id") => if_some_then(child.child_as_text(), |id| entry.id = id),

            (NS::Atom, "title") => entry.title = handle_text(child)?,

            (NS::Atom, "updated") => if_some_then(child.child_as_text(), |text| entry.updated = parser.parse_timestamp(&text)),

            (NS::Atom, "author") => if_some_then(handle_person("author", child)?, |person| entry.authors.push(person)),

            (NS::Atom, "content") => {
                entry.base = handle_base_attr(&child);
                entry.language = handle_language_attr(&child);
                entry.content = handle_content(child)?;
            }

            (NS::Atom, "link") => if_some_then(handle_link(child), |link| entry.links.push(link)),

            (NS::Atom, "summary") => entry.summary = handle_text(child)?,

            (NS::Atom, "category") => if_some_then(handle_category(child), |category| entry.categories.push(category)),

            (NS::Atom, "contributor") => if_some_then(handle_person("contributor", child)?, |person| entry.contributors.push(person)),

            // Some feeds have "pubDate" instead of "published"
            (NS::Atom, "published") | (NS::Atom, "pubDate") => if_some_then(child.child_as_text(), |text| entry.published = parser.parse_timestamp(&text)),

            (NS::Atom, "rights") => entry.rights = handle_text(child)?,

            // According to https://validator.w3.org/feed/docs/warning/CommentRSS.html we need to support both
            (NS::WellFormedWebComments, "commentRss") | (NS::WellFormedWebComments, "commentRSS") => {
                if_some_then(common::handle_link(Some(CommentsFeed), child), |link| entry.links.push(link))
            }

            // Handle MediaRSS elements
            (NS::MediaRSS, _) => parser.mediarss.handle_entry_mediarss_element(child)?,

            // Nothing required for unknown elements
            _ => {}
        }
    }

    // Handle completion of an entry, potentially emitting media objects if we've found them along the way
    parser.mediarss.entry_end(&mut entry);

    Ok(Some(entry))
}

// Handles an Atom <generator>
fn handle_generator<R: BufRead>(element: Element<R>) -> Option<Generator> {
    element.child_as_text().map(|content| {
        let mut generator = Generator::new(&content);

        for attr in element.attributes {
            match attr.name.as_str() {
                "uri" => generator.uri = Some(attr.value.clone()),
                "version" => generator.version = Some(attr.value.clone()),
                // Nothing required for unknown attributes
                _ => {}
            }
        }

        generator
    })
}

// Handles an Atom <icon> or <logo>
fn handle_image<R: BufRead>(element: Element<R>) -> Option<Image> {
    element
        .child_as_text()
        .map(|raw_uri| {
            util::parse_uri(&raw_uri, element.xml_base.as_ref())
                .map(|parsed| parsed.to_string())
                .unwrap_or(raw_uri)
        })
        .map(Image::new)
}

// Handles "xml:lang" as an attribute (e.g. in Atom feeds)
fn handle_language_attr<R: BufRead>(element: &Element<R>) -> Option<String> {
    element.attr_value("xml:lang")
}

// Handles an Atom <link>
pub(crate) fn handle_link<R: BufRead>(element: Element<R>) -> Option<Link> {
    // Always need an href
    element.attr_value("href").map(|href| {
        let mut link = Link::new(href, element.xml_base.as_ref());

        for attr in element.attributes {
            match attr.name.as_str() {
                "rel" => link.rel = Some(attr.value.clone()),
                "type" => link.media_type = Some(attr.value.clone()),
                "hreflang" => link.href_lang = Some(attr.value.clone()),
                "title" => link.title = Some(attr.value.clone()),
                "length" => link.length = attr.value.parse::<u64>().ok(),

                // Nothing required for unrecognised attributes
                _ => {}
            }
        }

        // Default "rel" to "alternate" if not set
        if link.rel.is_none() {
            link.rel = Some(String::from("alternate"));
        }

        link
    })
}

// Handles an Atom <author> or <contributor>
fn handle_person<R: BufRead>(role: &str, element: Element<R>) -> ParseFeedResult<Option<Person>> {
    let mut maybe_name = None;
    let mut maybe_uri = None;
    let mut maybe_email = None;
    for child in element.children() {
        let child = child?;
        let tag_name = child.name.as_str();
        let child_text = child.child_as_text();
        match (tag_name, child_text) {
            // Extract the fields from the spec
            ("name", Some(name)) => maybe_name = Some(name),
            ("uri", uri) => maybe_uri = uri,
            ("email", email) => maybe_email = email,

            // Nothing required for unknown elements
            _ => {}
        }
    }

    // Need either a name or an email
    let person = if maybe_name.is_some() || maybe_email.is_some() {
        let mut person = Person::empty();
        person.name = maybe_name;
        person.email = maybe_email;
        person.role = Some(role.into());
        person.uri = maybe_uri;
        Some(person)
    } else {
        None
    };

    Ok(person)
}

// Directly handles an Atom <title>, <summary>, <rights> or <subtitle> element
pub(crate) fn handle_text<R: BufRead>(element: Element<R>) -> ParseFeedResult<Option<Text>> {
    // Find type, defaulting to "text" if not present
    let type_attr = element.attributes.iter().find(|a| &a.name == "type").map_or("text", |a| a.value.as_str());

    let position = element.buffer_pos;
    match type_attr {
        "text" => {
            let s = element
                .child_as_text_sloppy()?
                .ok_or(ParseFeedError::ParseError(ParseErrorKind::MissingContent { position, reference: "text" }))?;

            let mut text = Text::new(s);
            text.content_type = MediaTypeBuf::new(names::TEXT, names::PLAIN);
            Ok(Some(text))
        }
        "html" | "text/html" => {
            let s = element
                .child_as_text_sloppy()?
                .ok_or(ParseFeedError::ParseError(ParseErrorKind::MissingContent { position, reference: "text" }))?;

            let mut text = Text::new(s);
            text.content_type = MediaTypeBuf::new(names::TEXT, names::HTML);
            Ok(Some(text))
        }
        "xhtml" => {
            let xml = element
                .children_as_xhtml()?
                .ok_or(ParseFeedError::ParseError(ParseErrorKind::MissingContent { position, reference: "text" }))?;

            let mut text = Text::new(xml);
            text.content_type = MediaTypeBuf::new(names::APPLICATION, names::XHTML);
            Ok(Some(text))
        }

        // Unknown content type
        _ => Err(ParseFeedError::ParseError(ParseErrorKind::UnknownMimeType {
            position,
            mime: type_attr.into(),
        })),
    }
}