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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use colored::*;

#[derive(Debug)]
pub enum Error {
    ReadError {
        path: std::path::PathBuf,
        io_error: std::io::Error,
    },
    WriteError {
        path: std::path::PathBuf,
        io_error: std::io::Error,
    },

    ProcessWaitError {
        io_error: std::io::Error,
    },
    ProcessOutError {
        io_error: std::io::Error,
    },

    MissingSetupPy {},
    MissingLock {},
    MissingVenv {
        path: std::path::PathBuf,
    },

    FileExists {
        path: std::path::PathBuf,
    },

    Other {
        message: String,
    },

    MalformedLock {
        line: usize,
        details: String,
    },

    NothingToBump {
        name: String,
    },

    MultipleBumps {
        name: String,
    },
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let message = match self {
            Error::Other { message } => message.to_string(),

            Error::ReadError { path, io_error } => {
                format!("could not read {}: {}", path.to_string_lossy(), io_error)
            }
            Error::WriteError { path, io_error } => {
                format!("could not write {}: {}", path.to_string_lossy(), io_error)
            }

            Error::ProcessWaitError { io_error } => {
                format!("could not wait for process: {}", io_error)
            }
            Error::ProcessOutError { io_error } => {
                format!("could not get process output: {}", io_error)
            }

            Error::MissingSetupPy {} => {
                "setup.py not found.\n You may want to run `dmenv init` now".to_string()
            }
            Error::MissingLock {} => {
                "requirements.lock not found.\n You may want to run `dmenv lock` now".to_string()
            }
            Error::MissingVenv { path } => {
                let mut message = format!(
                    "virtualenv in {} does not exist\n",
                    path.to_string_lossy().bold()
                );
                message.push_str("Please run `dmenv lock` or `dmenv install` to create it");
                message
            }

            Error::FileExists { path } => format!("{} already exist", path.to_string_lossy()),

            Error::MalformedLock { line, details } => {
                format!("Malformed lock at line {}\n:{}", line, details)
            }
            Error::NothingToBump { name } => format!("'{}' not found in lock", name),
            Error::MultipleBumps { name } => {
                format!("multiple matches found for '{}' in lock", name)
            }
        };
        write!(f, "{}", message)
    }
}