diff options
author | cel 🌸 <cel@blos.sm> | 2023-06-22 15:35:18 +0100 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-06-22 15:35:18 +0100 |
commit | a135acf943ba0fc442dca69f43339921728b1ed4 (patch) | |
tree | 77d8ad93d8064db1f0b71601e8b13b165c787cb9 /src/posts/mod.rs | |
parent | 139c26158bba4ec09e7ba690ba6c9c11da620707 (diff) | |
download | blossom-a135acf943ba0fc442dca69f43339921728b1ed4.tar.gz blossom-a135acf943ba0fc442dca69f43339921728b1ed4.tar.bz2 blossom-a135acf943ba0fc442dca69f43339921728b1ed4.zip |
implement blog tag filtering
Diffstat (limited to '')
-rw-r--r-- | src/posts/mod.rs | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/posts/mod.rs b/src/posts/mod.rs index be1e1a9..2dc5c4b 100644 --- a/src/posts/mod.rs +++ b/src/posts/mod.rs @@ -1,3 +1,6 @@ +mod article; +mod note; + use std::collections::HashSet; use async_trait::async_trait; @@ -16,12 +19,6 @@ enum PostType { Note, } -enum TextFormat { - Markdown, - Plaintext, - Html, -} - #[derive(Debug)] pub struct Post<D: Content> { // id: i64, @@ -34,7 +31,7 @@ pub struct Post<D: Content> { data: D, } -impl<D: Content + Serialize> Post<D> { +impl<D: Content> Post<D> { pub async fn render(&mut self) -> Result<()> { self.render = Some(self.data.render().await?); Ok(()) @@ -74,6 +71,8 @@ pub async fn get_blogposts() -> Result<Vec<Post<Article>>> { let blogpost = Post::try_from(blogpost).await.unwrap(); blogposts.push(blogpost); } + blogposts.sort_by_key(|post| post.created_at); + blogposts.reverse(); Ok(blogposts) } @@ -87,21 +86,45 @@ pub async fn get_blogpost(post_name: &str) -> Result<Post<Article>> { path: file.path().to_str().unwrap_or_default().to_owned(), name: name.to_owned(), }; - let blogpost = Post::try_from(blogpost).await.unwrap(); + let blogpost = Post::try_from(blogpost).await?; return Ok(blogpost); } } Err(BlossomError::NotFound(Status::new(404))) } -pub fn get_tags<D: Content>(posts: &Vec<Post<D>>) -> HashSet<String> { - posts.into_iter().fold(HashSet::new(), |mut acc, post| { - let tags = &post.tags; - for tag in tags { - acc.insert(tag.to_owned()); - } - acc - }) +pub fn get_tags<D: Content>(posts: &Vec<Post<D>>) -> Vec<&String> { + 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 +} + +pub fn filter_by_tags<'p, D: Content>( + posts: Vec<Post<D>>, + filter_tags: &HashSet<String>, +) -> Vec<Post<D>> { + posts + .into_iter() + .filter(|post| { + for tag in &post.tags { + match filter_tags.contains(tag) { + true => return true, + false => continue, + } + } + false + }) + .collect() } #[async_trait] |