summaryrefslogblamecommitdiff
path: root/src/posts/fs_post_repo.rs
blob: 13f797b9bbe5f4699796fffc1e9214a37b6933ca (plain) (tree)
1
                                           





















                                                                          







                                               
 
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()
    }
}