file_operation/move/sync/
fn.rs

1use crate::*;
2
3/// Moves a file from the source path to the destination path.
4///
5/// # Arguments
6///
7/// - `&str` - The source file path.
8/// - `&str` - The destination file path.
9///
10/// # Returns
11///
12/// - `Result<(), std::io::Error>` - Ok if the file was moved successfully, Err with error details otherwise.
13pub fn move_file(src: &str, dest: &str) -> Result<(), std::io::Error> {
14    std::fs::rename(src, dest)?;
15    Ok(())
16}
17
18/// Moves a directory and all its contents to another location.
19///
20/// # Arguments
21///
22/// - `&str` - The source directory path.
23/// - `&str` - The destination directory path.
24///
25/// # Returns
26///
27/// - `Result<(), std::io::Error>` - Ok if the directory was moved successfully, Err with error details otherwise.
28pub fn move_dir(src_dir: &str, dest_dir: &str) -> Result<(), std::io::Error> {
29    let src_path: &Path = Path::new(src_dir);
30    let dest_path: &Path = Path::new(dest_dir);
31    if dest_path.exists() {
32        std::fs::remove_dir_all(dest_path)?;
33    }
34    std::fs::create_dir_all(dest_path)?;
35    for entry in std::fs::read_dir(src_path)? {
36        let entry: std::fs::DirEntry = entry?;
37        let file_name: OsString = entry.file_name();
38        let src_file_path: PathBuf = entry.path();
39        let mut dest_file_path: PathBuf = PathBuf::from(dest_path);
40        dest_file_path.push(file_name);
41        if src_file_path.is_dir() {
42            move_dir(
43                src_file_path.to_str().unwrap(),
44                dest_file_path.to_str().unwrap(),
45            )?;
46        } else if src_file_path.is_file() {
47            std::fs::rename(&src_file_path, &dest_file_path)?;
48        }
49    }
50    std::fs::remove_dir(src_path)?;
51    Ok(())
52}