rss2email_lib/xml/
mod.rs

1//! Parses web feeds according to the RSS and Atom specifications and constructs
2//! [`Blog`]s and [`Post`](crate::blog::Post)s.
3
4use quick_xml::de::from_str;
5
6use crate::blog::Blog;
7
8use self::{atom::AtomFeed, error::ParserError, rss::RssFeed, traits::WebFeed};
9
10pub mod atom;
11pub mod error;
12pub mod rss;
13mod traits;
14
15/// Turns an XML feed into a `Blog` if possible.
16///
17/// First tries to parse it into an [`RssFeed`]. If that fails,
18/// it then tries to parse it into an [`AtomFeed`]. If both fail,
19/// the error is set to `Error1. Error2`.
20pub fn parse_web_feed(xml: &str) -> Result<Blog, ParserError> {
21  from_str::<RssFeed>(xml).into_blog().or_else(|e1| {
22    from_str::<AtomFeed>(xml)
23      .into_blog()
24      .map_err(|e2| ParserError::Parse(format!("{e1}\n{e2}")))
25  })
26}
27
28/// Limits the description to the amount of specified characters
29/// and postpends `...`.
30fn limit_description(desc: &str, limit: usize) -> String {
31  let mut res = desc.chars().take(limit).collect::<String>();
32
33  if desc.chars().count() >= limit {
34    res += "...";
35  }
36
37  res
38}