use crate::routes::posts; use std::collections::HashMap; use xml::escape::escape_str_pcdata; pub struct RssEntry { pub title: String, pub link: String, pub description: Option, pub created_at: String, pub author: String, pub guid: String, } impl From for RssEntry { fn from(post: posts::Post) -> Self { let web_url = std::env::var("BASE_URI_WEB").expect("Environment variable BASE_URI_WEB not found"); let post_url = format!("{}{}{}", web_url, "/posts/", post.post_id.to_string()); let author_full_name = format!("{} {}", post.first_name.unwrap(), post.last_name.unwrap()); Self { title: post.title.clone(), link: post_url.clone(), description: Some(post.body.clone()), created_at: post .created_at .expect("No timestamp was available") .to_rfc2822(), author: author_full_name, guid: post_url.clone(), } } } impl RssEntry { pub fn to_item(&self) -> String { format!( r#" <![CDATA[{}]]> {} {} {} "#, self.title, self.description.clone().unwrap_or_default(), self.created_at, self.guid, self.guid ) } } pub fn generate_rss( title: &str, description: &str, link: &str, posts: &HashMap, ) -> String { let values = posts.clone().into_values(); let rss_entries = values .map(|p| p.into()) .map(|r: RssEntry| r.to_item()) .collect::(); let safe_title = escape_str_pcdata(title); let safe_description = escape_str_pcdata(description); // TODO: change the atom link in this string - it's not correct // change it when we know the URL format!( r#" {safe_title} {safe_description} {link} en-us 60 Kyouma 1.0.0-SE {} "#, rss_entries ) }