use std::path::{Path, PathBuf};
pub fn list_folder_contents<P: AsRef<Path>>(path: P) -> Vec<PathBuf> {
let path = path.as_ref();
if !path.is_dir() {
panic!("The provided path is not a folder: {path:?}");
}
let mut entries = match std::fs::read_dir(path) {
Ok(entries) => entries
.filter_map(Result::ok)
.map(|e| e.path())
.collect::<Vec<PathBuf>>(),
Err(_) => panic!("Failed to read directory: {path:?}"),
};
entries.sort();
entries
}
#[cfg(test)]
mod tests {
use super::*;
use crate::save::save_string_to_file;
use crate::test_utils::get_temp_dir_path;
use tempfile::tempdir;
#[test]
fn test_list_folder_contents() {
let temp_dir = tempdir().unwrap();
let temp_dir_path = get_temp_dir_path(&temp_dir);
save_string_to_file("Content 1", temp_dir_path.join("file1.txt"));
save_string_to_file("Content 2", temp_dir_path.join("file2.txt"));
save_string_to_file("Content 3", temp_dir_path.join("subfolder/file3.txt"));
let contents = list_folder_contents(&temp_dir_path);
assert_eq!(
contents,
vec![
temp_dir_path.join("file1.txt"),
temp_dir_path.join("file2.txt"),
temp_dir_path.join("subfolder")
]
);
}
}