feedparser-rs 0.8.0

High-performance RSS/Atom/JSON Feed parser
Documentation
//! Shared document builders for the acceptance tests of the #628-#642 batch.

#![allow(
    dead_code,
    reason = "each test crate uses a different subset of the helpers"
)]

use feedparser_rs::{FeedError, ItunesFeedMeta, ParsedFeed, Result, parse};

const NS: &str = r#"xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/""#;

/// RSS 2.0 document with `channel` as the channel body.
pub fn rss(channel: &str) -> Result<ParsedFeed> {
    parse(format!(r#"<rss version="2.0" {NS}><channel>{channel}</channel></rss>"#).as_bytes())
}

/// RSS 2.0 document with one item holding `body`.
pub fn item(body: &str) -> Result<ParsedFeed> {
    rss(&format!("<item>{body}</item>"))
}

/// Atom document with `body` as the feed body.
pub fn atom(body: &str) -> Result<ParsedFeed> {
    parse(format!(r#"<feed xmlns="http://www.w3.org/2005/Atom" {NS}>{body}</feed>"#).as_bytes())
}

/// Atom document with one entry holding `body`.
pub fn entry(body: &str) -> Result<ParsedFeed> {
    atom(&format!("<entry>{body}</entry>"))
}

/// RSS 1.0 document with `channel` as the channel body.
pub fn rdf(channel: &str) -> Result<ParsedFeed> {
    parse(
        format!(
            r#"<rdf:RDF xmlns="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" {NS}><channel rdf:about="http://example.com/">{channel}</channel></rdf:RDF>"#
        )
        .as_bytes(),
    )
}

/// The feed-level iTunes block, or an error naming the missing block.
pub fn feed_itunes(feed: &ParsedFeed) -> Result<&ItunesFeedMeta> {
    feed.feed
        .itunes
        .as_deref()
        .ok_or_else(|| FeedError::Unknown("feed.itunes is missing".into()))
}