use std::io::{self, ErrorKind};
use std::path::Path;
use std::path::PathBuf;
pub fn children(path: &Path) -> Result<Vec<PathBuf>, io::Error> {
let mut children: Vec<PathBuf> = Vec::new();
let path = path.read_dir();
if let Ok(path) = path {
for child in path.flatten() {
let child_path = child.path();
children.push(child_path);
}
} else if let Err(path) = path {
eprintln!("Attempt to read contents of {path} has failed!")
}
Ok(children)
}
pub fn get_all_children(path: &Path) -> Result<Vec<PathBuf>, io::Error> {
let mut children: Vec<PathBuf> = Vec::new();
if path.try_exists()? {
get_all_driver(path, &mut children);
Ok(children)
} else {
Err(ErrorKind::NotFound.into())
}
}
fn get_all_driver(path: &Path, children: &mut Vec<PathBuf>) {
if path.is_dir() {
let path = path.read_dir();
if let Ok(path) = path {
for child in path.flatten() {
let child_path = child.path();
children.push(child_path.to_path_buf());
get_all_driver(&child_path, children);
}
} else if let Err(error) = path {
eprintln!(
"{}: Attempt to read contents of {:?} has failed!",
error.kind(),
children.last().unwrap()
);
children.pop();
}
}
}