rss

repository·master·Indexed 19 days ago

https://github.com/rust-syndication/rss

A Rust library for deserializing (reading) and serializing (writing) RSS web content syndication formats. It supports reading RSS versions 0.90, 0.91, 0.92, 1.0, and 2.0, while writing support is limited to RSS 2.0. The library includes support for extensions such as Dublin Core, Syndication, and iTunes, and provides optional features for builders and validation.

Tokens
8.6K
Snippets
38
Records
43
Agent score
67%

What's inside rss

  1. Read an RSS channel from a file or buffer

    master

    You can parse an RSS channel from any object that implements the BufRead trait using Channel::read_from. This includes reading from a file via BufReader or from a byte slice/buffer.

    use std::fs::File;
    use std::io::BufReader;
    use rss::Channel;
    
    let file = File::open("example.xml").unwrap();
    let channel = Channel::read_from(BufReader::new(file)).unwrap();
  2. Validate an RSS channel

    master

    You can validate the contents of a channel against the RSS specification using the validate() method. This requires enabling the validation feature.

    use rss::Channel;
    use rss::validation::Validate;
    
    let channel = Channel::default();
    channel.validate().unwrap();
  3. Create an RSS channel using ChannelBuilder

    master

    Use ChannelBuilder to construct a new RSS channel. This requires the builders feature, which is enabled by default.

    use rss::ChannelBuilder;
    
    let channel = ChannelBuilder::default()
        .title("Channel Title")
        .link("http://example.com")
        .description("An RSS feed.")
        .build()
        .unwrap();
  4. Read an RSS feed from a buffer

    master

    You can read an RSS channel from a byte buffer (e.g., data downloaded via HTTP) using Channel::read_from. This example demonstrates using reqwest to fetch a feed and parsing it from the resulting bytes.

    use std::error::Error;
    use rss::Channel;
    
    async fn example_feed() -> Result<Channel, Box<dyn Error>> {
        let content = reqwest::get("http://example.com/feed.xml")
            .await?
            .bytes()
            .await?;
        let channel = Channel::read_from(&content[..])?;
        Ok(channel)
    }
  5. Write an RSS channel to a writer or string

    master

    To serialize an RSS channel, you can use write_to to write to any object implementing the Write trait, or use to_string to convert the channel into an XML string. Note that writing support is currently limited to RSS 2.0.

    use rss::Channel;
    
    let channel = Channel::default();
    channel.write_to(::std::io::sink()).unwrap(); // write to the channel to a writer
    let string = channel.to_string(); // convert the channel to a string
  6. Read an RSS feed from a file

    master

    You can read an RSS channel from any object implementing the BufRead trait using Channel::read_from. For files, wrap a std::fs::File in a std::io::BufReader.

    use std::fs::File;
    use std::io::BufReader;
    use rss::Channel;
    
    let file = File::open("example.xml").unwrap();
    let channel = Channel::read_from(BufReader::new(file)).unwrap();
  7. Read an RSS channel from a buffer

    master

    To read from an in-memory buffer (such as bytes received from an HTTP request), pass a slice of the bytes to Channel::read_from.

    use std::error::Error;
    use rss::Channel;
    
    async fn example_feed() -> Result<Channel, Box<dyn Error>> {
        let content = reqwest::get("http://example.com/feed.xml")
            .await?
            .bytes()
            .await?;
        let channel = Channel::read_from(&content[..])?;
        Ok(channel)
    }
  8. Create a Category using CategoryBuilder

    master

    If you have the builders feature enabled, you can use the CategoryBuilder to construct a Category instance. This is useful for a more fluent API when setting multiple fields.

    use rss::Category;
    
    // Note: This requires the `builders` feature to be enabled
    let category = Category::builder()
        .name("Technology")
        .domain("http://example.com")
        .build();
  9. Build ITunesCategory using the Builder pattern

    master

    If the builders feature is enabled, you can use ITunesCategoryBuilder to construct an ITunesCategory more fluently. This is particularly useful for creating nested subcategories in a single expression.

    Example

    use rss::extension::itunes::ITunesCategory;
    
    // Create a nested category: music > pop
    let category = ITunesCategoryBuilder::default()
        .text("music")
        .subcategory(Some(Box::new(
            ITunesCategoryBuilder::default()
                .text("pop")
                .build()
        )))
        .build();
    use rss::extension::itunes::ITunesCategory;
    
    let category = ITunesCategoryBuilder::default()
        .text("music")
        .subcategory(Some(Box::new(
            ITunesCategoryBuilder::default()
                .text("pop")
                .build()
        )))
        .build();