diff options
Diffstat (limited to '')
-rw-r--r-- | src/posts/mod.rs | 55 | ||||
-rw-r--r-- | src/posts/note.rs | 8 |
2 files changed, 46 insertions, 17 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] diff --git a/src/posts/note.rs b/src/posts/note.rs index 95655d4..d7fa6c9 100644 --- a/src/posts/note.rs +++ b/src/posts/note.rs @@ -1 +1,7 @@ -struct note +enum TextFormat { + Markdown, + Plaintext, + Html, +} + +struct Note {} |