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
use std::{fmt::Display, io, path::PathBuf};

#[derive(Debug)]
pub enum RenameError {
    /// Errors originated by user input.
    InputError(InputError),

    /// General IO errors.
    Io(io::Error),
}

#[derive(Debug)]
pub enum InputError {
    /// Received --force and --interactive. Not sure how to continue.
    ForceAndInteractive,

    /// Cannot rename `file`. `directory` is already a directory.
    CannotRenameFileToDirectory(PathBuf, PathBuf),

    /// `file`. Not overwriting `file` without --interactive or --force.
    SkippingOverwrite(PathBuf, PathBuf),

    /// `path` is not a file. If this is intentional, pass --ignore-invalid-files.
    InvalidFile(PathBuf),

    /// Invalid rename. `file` can't be renamed to `file`.
    InvalidRename(PathBuf, PathBuf),
}

impl Display for RenameError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RenameError::InputError(err) => {
                let out = match err {
                    InputError::ForceAndInteractive => {
                        "Received --force and --interactive. Not sure how to continue.".to_string()
                    }
                    InputError::CannotRenameFileToDirectory(file, dir) => format!(
                        "Cannot rename {:?}. {:?} is already a directory.",
                        file, dir
                    ),
                    InputError::SkippingOverwrite(file, renamed) => format!(
                        "{:?}. Not overwriting {:?} without --interactive or --force.",
                        file, renamed,
                    ),
                    InputError::InvalidFile(path) => format!(
                        "{:?} is not a file. If this is intentional, pass --ignore-invalid-files.",
                        path
                    ),
                    InputError::InvalidRename(path, renamed) => format!(
                        "Invalid rename. {:?} can't be renamed to {:?}.",
                        path, renamed
                    ),
                };

                write!(f, "{}", out)
            }
            RenameError::Io(err) => write!(f, "IO error {}", err),
        }
    }
}

impl From<io::Error> for RenameError {
    fn from(e: io::Error) -> Self {
        RenameError::Io(e)
    }
}