gitignore_template_generator/fs/
impls.rs

1use std::{fs, io::Error};
2
3use super::FileSystemHandler;
4
5pub struct DirectoryHandler<'a> {
6    pub directory_path: &'a str,
7}
8
9impl<'a> DirectoryHandler<'a> {
10    pub fn new(directory_path: &'a str) -> Self {
11        Self { directory_path }
12    }
13}
14
15impl FileSystemHandler for DirectoryHandler<'_> {
16    fn fetch_content(&self, file_name: &str) -> Result<String, Error> {
17        fs::read_to_string(format!("{}/{file_name}", self.directory_path))
18    }
19
20    fn list_files(&self) -> Result<Vec<String>, Error> {
21        let mut result: Vec<String> = Vec::new();
22
23        for entry in fs::read_dir(self.directory_path)? {
24            let entry_path = entry?.path();
25
26            if entry_path.is_file() {
27                if let Some(file_stem) = entry_path.file_stem() {
28                    result.push(file_stem.to_string_lossy().to_string());
29                }
30            }
31        }
32
33        Ok(result)
34    }
35}