mineer/
lib.rs

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    // check if source exists
11    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    // create destination directory if it does not exist
19    if !destination.exists() {
20        DirBuilder::new().recursive(true).create(destination)?;
21    }
22
23    // iterate over the entries of the source directory
24    for entry in fs::read_dir(source)? {
25        let entry = entry?;
26        let entry_path = entry.path();
27
28        // create the corresponding destination path 
29        let destination_entry_path = destination.join(entry.file_name());
30
31        if entry_path.is_dir() {
32            // Recursively clone the subdirectory
33            clone_dir(&entry_path, &destination_entry_path)?;
34        } else {
35            // Copy the file
36            fs::copy(&entry_path, &destination_entry_path)?;
37        }
38    }
39
40
41
42
43    Ok(())
44}