diff options
author | Adam T. Carpenter <atc@53hor.net> | 2024-11-14 21:49:47 -0500 |
---|---|---|
committer | Adam T. Carpenter <atc@53hor.net> | 2024-11-14 21:49:47 -0500 |
commit | 4aa45ef3e7798ee18bea8b49af75e383afce02a1 (patch) | |
tree | 7a1353753d9300929b43a561ff2f1aae59b6434f /src/posts | |
parent | 014e34fa4a8cd4e3cdb3573a7748696c68873523 (diff) | |
parent | fc0e8296178ca779a270d91b681777f50b3b626d (diff) | |
download | carpentertutoring-master.tar.xz carpentertutoring-master.zip |
Diffstat (limited to 'src/posts')
-rw-r--r-- | src/posts/abstractions.rs | 2 | ||||
-rw-r--r-- | src/posts/abstractions/post.rs | 6 | ||||
-rw-r--r-- | src/posts/abstractions/repo.rs | 6 | ||||
-rw-r--r-- | src/posts/fs_post.rs | 47 | ||||
-rw-r--r-- | src/posts/fs_post_repo.rs | 32 |
5 files changed, 93 insertions, 0 deletions
diff --git a/src/posts/abstractions.rs b/src/posts/abstractions.rs new file mode 100644 index 0000000..96c8ced --- /dev/null +++ b/src/posts/abstractions.rs @@ -0,0 +1,2 @@ +pub mod post; +pub mod repo; diff --git a/src/posts/abstractions/post.rs b/src/posts/abstractions/post.rs new file mode 100644 index 0000000..a941281 --- /dev/null +++ b/src/posts/abstractions/post.rs @@ -0,0 +1,6 @@ +use std::{borrow::Cow, fmt}; + +pub trait Post: fmt::Debug + Ord { + fn get_title(&self) -> &str; + fn get_article(&self) -> Cow<str>; +} diff --git a/src/posts/abstractions/repo.rs b/src/posts/abstractions/repo.rs new file mode 100644 index 0000000..6fd5d08 --- /dev/null +++ b/src/posts/abstractions/repo.rs @@ -0,0 +1,6 @@ +use crate::posts::abstractions::post::Post; + +pub trait PostRepo { + fn load(&self) -> impl IntoIterator<Item = impl Post>; + fn by_id(&self, post_id: &str) -> impl Post; +} diff --git a/src/posts/fs_post.rs b/src/posts/fs_post.rs new file mode 100644 index 0000000..8b8e725 --- /dev/null +++ b/src/posts/fs_post.rs @@ -0,0 +1,47 @@ +use crate::posts::abstractions::post::Post; +use std::{borrow::Cow, fs, path::PathBuf}; + +#[derive(Debug, Eq)] +pub struct FsPost { + file: PathBuf, +} + +impl FsPost { + pub fn with_path(path: PathBuf) -> Self { + Self { file: path } + } +} + +impl Post for FsPost { + fn get_title(&self) -> &str { + self.file.file_stem().unwrap().to_str().unwrap() + } + + fn get_article(&self) -> Cow<str> { + let article = fs::read_to_string(&self.file).unwrap(); + 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/posts/fs_post_repo.rs b/src/posts/fs_post_repo.rs new file mode 100644 index 0000000..13f797b --- /dev/null +++ b/src/posts/fs_post_repo.rs @@ -0,0 +1,32 @@ +use crate::posts::abstractions::post::Post; +use crate::posts::abstractions::repo::PostRepo; +use crate::posts::fs_post::FsPost; +use std::{fs, path::PathBuf}; + +pub struct FsPostRepo { + dir: PathBuf, +} + +impl FsPostRepo { + pub fn with_dir(path: impl Into<PathBuf>) -> Self { + Self { dir: path.into() } + } +} + +impl PostRepo for FsPostRepo { + fn load(&self) -> impl IntoIterator<Item = FsPost> { + let files = fs::read_dir(&self.dir).unwrap(); + files + .flatten() + .filter(|d| !d.file_name().to_string_lossy().starts_with('.')) + .map(|d| FsPost::with_path(d.path())) + } + + fn by_id(&self, post_id: &str) -> FsPost { + let posts = self.load(); + posts + .into_iter() + .find(|p| p.get_title() == post_id) + .unwrap() + } +} |