X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/1c5797fadeea6be505c01f13508203ba234cbdfa..3f1aa0b6eb90bd7912a63c6b72c2571486fbc21f:/src/file_finder.rs diff --git a/src/file_finder.rs b/src/file_finder.rs index 43eee33..ef57950 100644 --- a/src/file_finder.rs +++ b/src/file_finder.rs @@ -3,65 +3,59 @@ use std::fs::read_dir; use std::path::PathBuf; pub fn find_files(directory_path: &PathBuf) -> Vec { - return find_files_recursively(directory_path, directory_path); + find_files_recursively(directory_path, directory_path) } fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec { let mut result: Vec = vec![]; let file_handler = FileHandler::default(); - let entries = read_dir(&directory_path).unwrap(); + 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(); + 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)) + 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, - }); + result.push(File { path, file_type }); } } - return result; + result } - #[cfg(test)] mod tests { use std::collections::HashSet; - use std::path::PathBuf; use std::fs::create_dir_all; + use std::path::PathBuf; use super::*; - use crate::file_handler::FileType; use crate::file_handler::File; + use crate::file_handler::FileType; use test_utilities::*; - fn get_paths(root_directory: &PathBuf, files: &Vec) -> HashSet { + fn get_paths(root_directory: &PathBuf, files: &[File]) -> HashSet { files .iter() - .map( |file| + .map(|file| { file.path .strip_prefix(root_directory) .unwrap() .to_string_lossy() .to_string() - ) + }) .collect() } #[test] fn finds_all_files() { let test_dir = setup_test_dir(); - create_dir_all(&test_dir.join("nested")) - .expect("Could not create nested test directory"); - create_dir_all(&test_dir.join("assets")) - .expect("Could not create assets test directory"); + create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory"); + create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory"); create_test_file(&test_dir.join("test1.gmi"), ""); create_test_file(&test_dir.join("_layout.html"), ""); create_test_file(&test_dir.join("nested/nested.gmi"), ""); @@ -81,11 +75,10 @@ mod tests { #[test] fn identifies_correct_file_types() { let test_dir = setup_test_dir(); - create_dir_all(&test_dir.join("nested")) - .expect("Could not create nested test directory"); - create_dir_all(&test_dir.join("assets")) - .expect("Could not create assets test directory"); + create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory"); + create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory"); create_test_file(&test_dir.join("_layout.html"), ""); + create_test_file(&test_dir.join("notalayout.html"), ""); create_test_file(&test_dir.join("nested/nested.gmi"), ""); create_test_file(&test_dir.join("assets/style.css"), ""); create_test_file(&test_dir.join("image.png"), ""); @@ -97,11 +90,11 @@ mod tests { Some("gmi") => assert_eq!(file.file_type, FileType::Gemini), Some("html") => { if file.path.ends_with("_layout.html") { - assert_eq!(file.file_type, FileType::Layout) + assert_eq!(file.file_type, FileType::Layout); } else { - assert_eq!(file.file_type, FileType::File) + assert_eq!(file.file_type, FileType::File); } - }, + } _ => assert_eq!(file.file_type, FileType::File), } } @@ -110,12 +103,9 @@ mod tests { #[test] fn ignores_git_directory() { let test_dir = setup_test_dir(); - create_dir_all(&test_dir.join("nested")) - .expect("Could not create nested test directory"); - create_dir_all(&test_dir.join("assets")) - .expect("Could not create assets test directory"); - create_dir_all(&test_dir.join(".git")) - .expect("Could not create git test directory"); + create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory"); + create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory"); + create_dir_all(test_dir.join(".git")).expect("Could not create git test directory"); create_test_file(&test_dir.join("_layout.html"), ""); create_test_file(&test_dir.join("nested/nested.gmi"), ""); create_test_file(&test_dir.join("assets/style.css"), "");