diff options
author | Adam T. Carpenter <atc@53hor.net> | 2024-09-14 21:11:51 -0400 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2024-09-14 21:11:51 -0400 |
commit | aad33fee01abe817d82c094f83ffd44ca36c8456 (patch) | |
tree | 9db9740d84f97575775cfe832a69ac2eed7d2078 | |
parent | 18339f611fd17e1300593edd65adf7604a39ad72 (diff) | |
download | carpentertutoring-aad33fee01abe817d82c094f83ffd44ca36c8456.tar.xz carpentertutoring-aad33fee01abe817d82c094f83ffd44ca36c8456.zip |
feat: reverse sort posts by modify datetime
-rw-r--r-- | src/posts/abstractions/post.rs | 2 | ||||
-rw-r--r-- | src/posts/fs_post.rs | 25 | ||||
-rw-r--r-- | src/views/posts.rs | 7 | ||||
-rw-r--r-- | templates/base.html | 1 |
4 files changed, 30 insertions, 5 deletions
diff --git a/src/posts/abstractions/post.rs b/src/posts/abstractions/post.rs index 6d504db..a941281 100644 --- a/src/posts/abstractions/post.rs +++ b/src/posts/abstractions/post.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, fmt}; -pub trait Post: fmt::Debug { +pub trait Post: fmt::Debug + Ord { fn get_title(&self) -> &str; fn get_article(&self) -> Cow<str>; } diff --git a/src/posts/fs_post.rs b/src/posts/fs_post.rs index f83ff4a..8b8e725 100644 --- a/src/posts/fs_post.rs +++ b/src/posts/fs_post.rs @@ -1,7 +1,7 @@ use crate::posts::abstractions::post::Post; use std::{borrow::Cow, fs, path::PathBuf}; -#[derive(Debug)] +#[derive(Debug, Eq)] pub struct FsPost { file: PathBuf, } @@ -22,3 +22,26 @@ impl Post for FsPost { Cow::Owned(article) } } + +impl Ord for FsPost { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.file + .metadata() + .unwrap() + .modified() + .unwrap() + .cmp(&other.file.metadata().unwrap().modified().unwrap()) + } +} + +impl PartialOrd for FsPost { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + Some(self.cmp(other)) + } +} + +impl PartialEq for FsPost { + fn eq(&self, other: &Self) -> bool { + self.get_title() == other.get_title() + } +} diff --git a/src/views/posts.rs b/src/views/posts.rs index 2ce69f8..82b5996 100644 --- a/src/views/posts.rs +++ b/src/views/posts.rs @@ -10,8 +10,9 @@ pub struct PostsView<P: Post> { impl<P: Post> PostsView<P> { pub fn with_posts(posts: impl IntoIterator<Item = P>) -> Self { - Self { - posts: posts.into_iter().collect(), - } + let mut posts: Vec<P> = posts.into_iter().collect(); + posts.sort(); + posts.reverse(); + Self { posts } } } diff --git a/templates/base.html b/templates/base.html index f146a44..79ba662 100644 --- a/templates/base.html +++ b/templates/base.html @@ -33,6 +33,7 @@ <a href="/policies">Policies</a> <a href="/#pricing">Pricing</a> <a href="/brochure">Brochure</a> + <a href="/posts">Posts</a> <a href="/#offerings">Offerings</a> <a href="/#"><img alt="logo" src="/assets/logo-simple.png" /></a> </nav> |