use chrono::{DateTime, Utc}; use htmlescape::encode_minimal; use config::Config; use entry::{Entry, EntryKind}; pub fn generate_post_rss(config: &Config, post: &Entry) -> String { let date: DateTime = DateTime::from_utc(post.date.unwrap().and_hms(0, 0, 0), Utc); let date_string = date.format("%a, %d %b %Y %H:%M:%S %z").to_string(); let url = format!("{}/posts/{}/", config.url, post.slug); format!( "{}{}{}{}{}", post.title, encode_minimal(&post.render("
{{ body }}
", config)), url, url, date_string ) } pub fn generate_rss(config: &Config, posts: &Vec) -> String { let items = posts .into_iter() .filter(|post| post.kind == EntryKind::Post) .map(|post| generate_post_rss(&config, &post)) .collect::>() .join(""); format!( "{}{}{}{}", config.url, config.site_name, config.description, config.url, items ) } #[cfg(test)] mod tests { use chrono::NaiveDate; use super::generate_rss; use config::Config; use entry::{Entry, EntryKind}; #[test] fn generates_rss_feed() { let config = Config { site_name: String::from("Lorem Ipsum"), url: String::from("https://www.loremipsum.com"), description: String::from("recent posts from loremipsum.com"), }; let posts: Vec = vec![Entry { title: String::from("Hello World"), body: String::from("lorem ipsum dolor sit amet"), slug: String::from("hello-world"), date: Some(NaiveDate::from_ymd(2019, 1, 1)), kind: EntryKind::Post, }]; assert_eq!( generate_rss(&config, &posts), "Lorem Ipsumrecent posts from loremipsum.comhttps://www.loremipsum.comHello World<article><p>lorem ipsum dolor sit amet</p>\n</article>https://www.loremipsum.com/posts/hello-world/https://www.loremipsum.com/posts/hello-world/Tue, 01 Jan 2019 00:00:00 +0000" ); } }