use std::fs;
use std::io;
use std::path::{Path, PathBuf};
pub fn walk_files(dir: &Path) -> Result<Vec<PathBuf>, io::Error> {
let mut paths = Vec::new();
_walk_files(&mut paths, dir)?;
Ok(paths)
}
fn _walk_files(paths: &mut Vec<PathBuf>, path: &Path) -> Result<(), io::Error> {
if path.is_dir() {
for child in fs::read_dir(path)? {
let child = child?;
_walk_files(paths, &child.path())?;
}
} else {
paths.push(path.to_path_buf());
}
Ok(())
}
pub fn list_files(path: &Path) -> Result<Vec<PathBuf>, io::Error> {
let mut paths = Vec::new();
for child in fs::read_dir(path)? {
let child = child?;
if child.file_type()?.is_file() {
paths.push(child.path());
}
}
Ok(paths)
}
pub fn write_file_deep<P: AsRef<Path>, C: AsRef<[u8]>>(
path: P,
content: C,
) -> Result<(), io::Error> {
let path = path.as_ref();
let parent = path.parent().ok_or_else(|| io::Error::other("no parent"))?;
fs::create_dir_all(parent)?;
fs::write(path, content)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
#[test]
fn test_walk_dir() -> Result<(), io::Error> {
let temp_dir = tempdir()?;
let temp_path = temp_dir.path();
let subdir1 = temp_path.join("subdir1");
let subdir2 = temp_path.join("subdir1/subdir2");
fs::create_dir(&subdir1)?;
fs::create_dir(&subdir2)?;
fs::write(temp_path.join("file1.txt"), b"content1")?;
fs::write(subdir1.join("file2.txt"), b"content2")?;
fs::write(subdir2.join("file3.txt"), b"content3")?;
let paths = walk_files(temp_path)?;
assert_eq!(paths.len(), 3);
assert!(paths.contains(&temp_path.join("file1.txt")));
assert!(paths.contains(&subdir1.join("file2.txt")));
assert!(paths.contains(&subdir2.join("file3.txt")));
Ok(())
}
#[test]
fn test_write_file_deep() -> Result<(), io::Error> {
let temp_dir = tempdir()?;
let temp_path = temp_dir.path();
let new_file_path = temp_path.join("new_directory/new_file.txt");
let new_file_content = b"new file content";
write_file_deep(&new_file_path, new_file_content)?;
assert!(new_file_path.exists());
assert_eq!(fs::read_to_string(new_file_path)?, "new file content");
Ok(())
}
}