From 394b2630b229298e8ad32fc12ac5642d72a7e023 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Sun, 28 Aug 2022 11:22:56 +1000 Subject: [PATCH] Warn instead of failing if summary selector doesn't match --- src/main.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8d95965..058da2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -295,12 +295,13 @@ async fn process_feed( let link = attrs .get("href") .ok_or_else(|| eyre!("element selected as heading has no 'href' attribute"))?; - let description = extract_description(config, &item)?; + let title_text = title.text_contents(); + let description = extract_description(config, &item, &title_text)?; let date = extract_pub_date(config, &item)?; let guid = GuidBuilder::default().value(link).permalink(false).build(); let rss_item = ItemBuilder::default() - .title(title.text_contents()) + .title(title_text) .link(base_url.parse(link).ok().map(|u| u.to_string())) .guid(Some(guid)) .pub_date(date.map(|date| date.to_rfc2822())) @@ -409,23 +410,31 @@ fn trim_date(s: &str) -> &str { fn extract_description( config: &FeedConfig, item: &NodeDataRef, + title: &str, ) -> eyre::Result> { - config - .summary - .as_ref() - .map(|selector| { - item.as_node() - .select_first(selector) - .map_err(|()| eyre!("invalid selector for summary: {}", selector)) - .and_then(|node| { - let mut text = Vec::new(); - node.as_node() - .serialize(&mut text) - .wrap_err("unable to serialise description") - .map(|()| String::from_utf8(text).unwrap()) // NOTE(unwrap): Should be safe as XML has to be legit Unicode) - }) - }) - .transpose() + let node = config.summary.as_ref().and_then(|selector| { + item.as_node() + .select_first(selector) + .map_err(|()| { + warn!( + "summary selector for item with title '{}' did not match anything", + title.trim() + ) + }) + .ok() + }); + if node.is_none() { + return Ok(None); + } + + node.map(|node| { + let mut text = Vec::new(); + node.as_node() + .serialize(&mut text) + .wrap_err("unable to serialise description") + .map(|()| String::from_utf8(text).unwrap()) // NOTE(unwrap): Should be safe as XML has to be legit Unicode) + }) + .transpose() } fn deserialise_cached_headers(path: &Path) -> Option> {