aboutsummaryrefslogtreecommitdiff
path: root/src/file_finder.rs
blob: bcd368dbf94089280c36010ac35f51c6b4618aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use crate::file_handler::{File, FileHandler};
use std::fs::read_dir;
use std::path::PathBuf;

pub fn find_files(directory_path: &PathBuf) -> Vec<File> {
    return find_files_recursively(directory_path, directory_path);
}

fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec<File> {
    let mut result: Vec<File> = vec![];
    let file_handler = FileHandler::default();
    let entries = read_dir(&directory_path).unwrap();
    for entry in entries {
        let path = entry.unwrap().path();
        let relative_path = path.strip_prefix(&root_path).unwrap();
        if relative_path.starts_with(".git") || relative_path.starts_with(".gitignore") {
            continue;
        }
        if path.is_dir() {
            result.append(&mut find_files_recursively(&root_path, &path))
        } else {
            let file_type = file_handler.identify(&path);
            result.push(File {
                path: path,
                file_type: file_type,
            });
        }
    }
    return result;
}