summaryrefslogtreecommitdiff
path: root/src/posts/fs_post_repo.rs
blob: eb37a6a71b7b124a8118c6102eefccec5d0421f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()))
    }
}