1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();

    // check if source exists
    if !source.is_dir() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "the source path is not a directory",
        ));
    }

    // create destination directory if it does not exist
    if !destination.exists() {
        DirBuilder::new().recursive(true).create(destination)?;
    }

    // iterate over the entries of the source directory
    for entry in fs::read_dir(source)? {
        let entry = entry?;
        let entry_path = entry.path();

        // create the corresponding destination path 
        let destination_entry_path = destination.join(entry.file_name());

        if entry_path.is_dir() {
            // Recursively clone the subdirectory
            clone_dir(&entry_path, &destination_entry_path)?;
        } else {
            // Copy the file
            fs::copy(&entry_path, &destination_entry_path)?;
        }
    }




    Ok(())
}