1use std::fs::{self, DirBuilder};
2use std::io;
3use std::path::{Path, PathBuf};
4
5
6pub fn clone_dir(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
7 let source = source.as_ref();
8 let destination = destination.as_ref();
9
10 if !source.is_dir() {
12 return Err(io::Error::new(
13 io::ErrorKind::InvalidInput,
14 "the source path is not a directory",
15 ));
16 }
17
18 if !destination.exists() {
20 DirBuilder::new().recursive(true).create(destination)?;
21 }
22
23 for entry in fs::read_dir(source)? {
25 let entry = entry?;
26 let entry_path = entry.path();
27
28 let destination_entry_path = destination.join(entry.file_name());
30
31 if entry_path.is_dir() {
32 clone_dir(&entry_path, &destination_entry_path)?;
34 } else {
35 fs::copy(&entry_path, &destination_entry_path)?;
37 }
38 }
39
40
41
42
43 Ok(())
44}