aboutsummaryrefslogtreecommitdiffstats
path: root/src/posts.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2024-01-30 16:16:05 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2024-01-30 16:16:05 +0000
commitf00159f53b3774601500ec65345791311ff6efa1 (patch)
treecc65a487e71a056126e1935108a494c7851863f9 /src/posts.rs
parent8c03d9a53f4bc2f70fb5c1e0487bc74fe0137fcb (diff)
downloadblossom-f00159f53b3774601500ec65345791311ff6efa1.tar.gz
blossom-f00159f53b3774601500ec65345791311ff6efa1.tar.bz2
blossom-f00159f53b3774601500ec65345791311ff6efa1.zip
migrate to poem and askama
Diffstat (limited to 'src/posts.rs')
-rw-r--r--src/posts.rs62
1 files changed, 62 insertions, 0 deletions
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<Utc>;
+ fn updated_at(&self) -> &DateTime<Utc>;
+ fn tags(&self) -> &Vec<String>;
+ 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<Self>) -> 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::<Vec<_>>();
+ tags.sort();
+ tags
+ }
+
+ fn filter_by_tags(posts: Vec<Self>, filter_tags: &HashSet<String>) -> Vec<Self>
+ 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()
+ }
+}