summaryrefslogtreecommitdiff
path: root/src/posts
diff options
context:
space:
mode:
Diffstat (limited to 'src/posts')
-rw-r--r--src/posts/abstractions.rs2
-rw-r--r--src/posts/abstractions/post.rs6
-rw-r--r--src/posts/abstractions/repo.rs5
-rw-r--r--src/posts/fs_post.rs24
-rw-r--r--src/posts/fs_post_repo.rs23
5 files changed, 60 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..6d504db
--- /dev/null
+++ b/src/posts/abstractions/post.rs
@@ -0,0 +1,6 @@
+use std::{borrow::Cow, fmt};
+
+pub trait Post: fmt::Debug {
+ 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..6fcb385
--- /dev/null
+++ b/src/posts/abstractions/repo.rs
@@ -0,0 +1,5 @@
+use crate::posts::abstractions::post::Post;
+
+pub trait PostRepo {
+ fn load(&self) -> impl IntoIterator<Item = impl Post>;
+}
diff --git a/src/posts/fs_post.rs b/src/posts/fs_post.rs
new file mode 100644
index 0000000..e767803
--- /dev/null
+++ b/src/posts/fs_post.rs
@@ -0,0 +1,24 @@
+use crate::posts::abstractions::post::Post;
+use std::{borrow::Cow, fs, path::PathBuf};
+
+#[derive(Debug)]
+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_name().unwrap().to_str().unwrap()
+ }
+
+ fn get_article(&self) -> Cow<str> {
+ let article = fs::read_to_string(&self.file).unwrap();
+ Cow::Owned(article)
+ }
+}
diff --git a/src/posts/fs_post_repo.rs b/src/posts/fs_post_repo.rs
new file mode 100644
index 0000000..eb37a6a
--- /dev/null
+++ b/src/posts/fs_post_repo.rs
@@ -0,0 +1,23 @@
+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()))
+ }
+}