// Copyright ยฉ 2024 RSS Gen. All rights reserved. // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT // See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full license information. //! # RSS Gen Parser Usage Examples //! //! This example demonstrates how to parse RSS feeds from XML content using the RSS Gen library. #![allow(missing_docs)] use rss_gen::parse_rss; use std::error::Error; /// Custom error type for example execution #[derive(Debug)] struct ExampleError { message: String, } impl std::fmt::Display for ExampleError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Example Error: {}", self.message) } } impl Error for ExampleError {} /// Entry point for the RSS Gen parser examples. /// /// This function demonstrates parsing XML content into structured RSS data. pub fn main() -> Result<(), Box> { println!("\n๐Ÿงช RSS Gen Parser Usage Examples \n"); // Run the example for RSS feed parsing parse_rss_example()?; parse_rss_0_90_example()?; parse_rss_0_91_example()?; parse_rss_0_92_example()?; parse_rss_1_0_example()?; parse_rss_2_0_example()?; println!("\n๐ŸŽ‰ All examples completed successfully!\n"); Ok(()) } /// Demonstrates parsing an RSS 2.0 feed from XML content. fn parse_rss_example() -> Result<(), Box> { println!("๐Ÿฆ€ Parse Rss 2.0 Feed Example"); println!("---------------------------------------------"); let xml_content = r#" Rss 2.0 Sample customer product catalog feed http://www.yourdomain.com The latest product catalog feed of sample customer. Sample Coat 001 http://yourdomain.com/c001.aspx c001 htto://yourdomain.com/image/c001.jpg Sun, 20 Apr 2008 00:00:00 GMT High quality wool coat. Winter Wear]]> Cashmere]]> SampleManufactor1 Y 20 Y short sleeve,blue,men's,outdoor h001,h003 Black,Grey 10, 12, 14 10 8.50 GBP Sample Hat 002 http://yourdomain.com/h002.aspx h002 http://yourdomain.com/image/h002.jpg Wed, 30 Apr 2008 00:00:00 GMT Low quality wool hat. Wool Hat]]> SampleManufactor2 Y 20 Y short sleeve,blue,men's,outdoor h001,h003 Black 16,18,20 5 GBP "#; // Parse the RSS content let parsed_data = parse_rss(xml_content, None).map_err(|e| { Box::new(ExampleError { message: format!("Failed to parse RSS feed: {}", e), }) as Box })?; // Pretty-print the entire parsed data using the Debug trait println!(" โœ… Parsed RSS feed data: {:#?}", parsed_data); // Directly access individual fields of parsed_data println!(" โœ… Parsed RSS feed title: {:?}", parsed_data.title); println!(" โœ… Parsed RSS feed link: {:?}", parsed_data.link); println!( " โœ… Parsed RSS feed description: {:?}", parsed_data.description ); println!(" โœ… Number of items: {}", parsed_data.items.len()); // Print details of the first item, if available if let Some(first_item) = parsed_data.items.first() { println!(" โœ… First item title: {:?}", first_item.title); println!(" โœ… First item link: {:?}", first_item.link); println!( " โœ… First item description: {:?}", first_item.description ); } Ok(()) } fn parse_rss_0_90_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Parsing RSS 0.90 Example"); let rss_0_90_content = r#" Mozilla Dot Org http://www.mozilla.org the Mozilla Organization web site Mozilla http://www.mozilla.org/images/moz.gif http://www.mozilla.org New Status Updates http://www.mozilla.org/status/ Bugzilla Reorganized http://www.mozilla.org/bugs/ "#; let parsed_data = parse_rss(rss_0_90_content, None)?; println!("Parsed RSS 0.90 feed:"); println!("Title: {}", parsed_data.title); println!("Link: {}", parsed_data.link); println!("Description: {}", parsed_data.description); println!("Number of items: {}", parsed_data.items.len()); Ok(()) } fn parse_rss_0_91_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Parsing RSS 0.91 Example"); let rss_0_91_content = r#" XML.com http://www.xml.com/ XML.com features a rich mix of information and services for the XML community. en-us Normalizing XML, Part 2 http://www.xml.com/pub/a/2002/12/04/normalizing.html In this second and final look at applying relational normalization techniques to W3C XML Schema data modeling, Will Provost discusses when not to normalize, the scope of uniqueness and the fourth and fifth normal forms. "#; let parsed_data = parse_rss(rss_0_91_content, None)?; println!("Parsed RSS 0.91 feed:"); println!("Title: {}", parsed_data.title); println!("Link: {}", parsed_data.link); println!("Description: {}", parsed_data.description); println!("Language: {}", parsed_data.language); println!("Number of items: {}", parsed_data.items.len()); Ok(()) } fn parse_rss_0_92_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Parsing RSS 0.92 Example"); let rss_0_92_content = r#" My Website http://www.example.com/ News and updates from my website. en-us Mon, 11 Oct 2024 21:57:00 GMT My Website Image http://www.example.com/image.jpg http://www.example.com/ First article title http://www.example.com/article1 Short description of the article. Mon, 11 Oct 2024 12:00:00 GMT "#; let parsed_data = parse_rss(rss_0_92_content, None)?; println!("Parsed RSS 0.92 feed:"); println!("Title: {}", parsed_data.title); println!("Link: {}", parsed_data.link); println!("Description: {}", parsed_data.description); println!("Last Build Date: {}", parsed_data.last_build_date); println!("Number of items: {}", parsed_data.items.len()); Ok(()) } fn parse_rss_1_0_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Parsing RSS 1.0 Example"); let rss_1_0_content = r#" XML.com http://www.xml.com/ XML.com features a rich mix of information and services for the XML community. en-us Normalizing XML, Part 2 http://www.xml.com/pub/a/2002/12/04/normalizing.html In this second and final look at applying relational normalization techniques to W3C XML Schema data modeling, Will Provost discusses when not to normalize, the scope of uniqueness and the fourth and fifth normal forms. Will Provost 2002-12-04 "#; let parsed_data = parse_rss(rss_1_0_content, None)?; println!("Parsed RSS 1.0 feed:"); println!("Title: {}", parsed_data.title); println!("Link: {}", parsed_data.link); println!("Description: {}", parsed_data.description); println!("Language: {}", parsed_data.language); println!("Number of items: {}", parsed_data.items.len()); Ok(()) } fn parse_rss_2_0_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Parsing RSS 2.0 Example"); let rss_2_0_content = r#" XML.com http://www.xml.com/ XML.com features a rich mix of information and services for the XML community. en-us Normalizing XML, Part 2 http://www.xml.com/pub/a/2002/12/04/normalizing.html In this second and final look at applying relational normalization techniques to W3C XML Schema data modelling, Will Provost discusses when not to normalize, the scope of uniqueness and the fourth and fifth normal forms. Will Provost 2002-12-04 "#; let parsed_data = parse_rss(rss_2_0_content, None)?; println!("Parsed RSS 2.0 feed:"); println!("Title: {}", parsed_data.title); println!("Link: {}", parsed_data.link); println!("Description: {}", parsed_data.description); println!("Language: {}", parsed_data.language); println!("Number of items: {}", parsed_data.items.len()); Ok(()) }