From f00159f53b3774601500ec65345791311ff6efa1 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 30 Jan 2024 16:16:05 +0000 Subject: migrate to poem and askama --- src/posts.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/posts.rs (limited to 'src/posts.rs') diff --git a/src/posts.rs b/src/posts.rs new file mode 100644 index 0000000..415f743 --- /dev/null +++ b/src/posts.rs @@ -0,0 +1,62 @@ +use std::collections::HashSet; + +use chrono::{DateTime, Utc}; +use serde::Serialize; + +#[derive(Serialize, Debug)] +pub enum PostType { + Article, + Note, +} + +pub trait Post { + fn id(&self) -> &str; + fn subject(&self) -> Option<&str>; + fn published_at(&self) -> &DateTime; + fn updated_at(&self) -> &DateTime; + fn tags(&self) -> &Vec; + fn lang(&self) -> &str; + fn post_type(&self) -> PostType; + fn content(&self) -> &str; + + fn link(&self) -> String { + "https://en.blos.sm/posts/".to_owned() + self.id() + } + + fn get_tags<'a>(posts: &'a Vec) -> Vec<&'a String> + where + Self: Sized, + { + let mut tags = posts + .into_iter() + .fold(HashSet::new(), |mut acc, post| { + let tags = post.tags(); + for tag in tags { + acc.insert(tag); + } + acc + }) + .into_iter() + .collect::>(); + tags.sort(); + tags + } + + fn filter_by_tags(posts: Vec, filter_tags: &HashSet) -> Vec + where + Self: Sized, + { + posts + .into_iter() + .filter(|post| { + for tag in post.tags() { + match filter_tags.contains(tag) { + true => return true, + false => continue, + } + } + false + }) + .collect() + } +} -- cgit