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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::path::{Path, PathBuf};

mod split;

pub use split::{split, unsplit};

#[derive(Debug)]
pub enum ReorgError {
    NoFilesReturned,
}

impl std::error::Error for ReorgError {}
impl std::fmt::Display for ReorgError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use ReorgError::*;

        write!(
            f,
            "{}",
            match self {
                NoFilesReturned => "No Files Found",
            }
        )
    }
}

pub trait FileMover {
		fn relocate(&self, source_file: &Path, destination_directory: &Path) -> std::io::Result<()>;
		fn remove(&self, p: &Path) -> std::io::Result<()>;
}

fn get_destination_path(source: &Path, destination: &Path) -> std::io::Result<PathBuf> {
    let base_filename = source
        .file_name()
        .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "Unable to get filename"))?;
    Ok(PathBuf::from(destination).join(base_filename))
}

pub struct DryRunFileMover {}
impl FileMover for DryRunFileMover {
    fn relocate(&self, source_file: &Path, destination: &Path) -> std::io::Result<()> {
        let destination = get_destination_path(source_file, destination)?;
        println!("{} -> {}", source_file.display(), destination.display());
        Ok(())
    }

    fn remove(&self, p: &Path) -> std::io::Result<()> {
        Ok(()) // This is a no-op for dry runs
    }
}

pub struct OsFileMover {}
impl FileMover for OsFileMover {
    fn relocate(&self, source_file: &Path, destination: &Path) -> std::io::Result<()> {
        let destination = get_destination_path(source_file, destination)?;
        let parent = destination.parent().ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::Other, "Unable to get filename")
        })?;
        std::fs::create_dir_all(parent)?;
        std::fs::rename(source_file, &destination)?;
        println!("{} -> {}", source_file.display(), destination.display());
        Ok(())
		}

		fn remove(&self, p: &Path) -> std::io::Result<()> {
			std::fs::remove_dir(p)
		}
}