From aad33fee01abe817d82c094f83ffd44ca36c8456 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 14 Sep 2024 21:11:51 -0400 Subject: feat: reverse sort posts by modify datetime --- src/posts/abstractions/post.rs | 2 +- src/posts/fs_post.rs | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'src/posts') 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; } 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 { + Some(self.cmp(other)) + } +} + +impl PartialEq for FsPost { + fn eq(&self, other: &Self) -> bool { + self.get_title() == other.get_title() + } +} -- cgit v1.2.3