use std::fs::{self, DirBuilder};
use std::io;
use std::path::{Path, PathBuf};
pub fn clone_dir(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
let source = source.as_ref();
let destination = destination.as_ref();
if !source.is_dir() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"the source path is not a directory",
));
}
if !destination.exists() {
DirBuilder::new().recursive(true).create(destination)?;
}
for entry in fs::read_dir(source)? {
let entry = entry?;
let entry_path = entry.path();
let destination_entry_path = destination.join(entry.file_name());
if entry_path.is_dir() {
clone_dir(&entry_path, &destination_entry_path)?;
} else {
fs::copy(&entry_path, &destination_entry_path)?;
}
}
Ok(())
}