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
#[derive(Debug)]
pub struct CliError {
    error: Option<anyhow::Error>,
    code: i32,
}

impl CliError {
    pub fn silent(code: i32) -> Self {
        Self { error: None, code }
    }

    pub fn message(e: impl Into<anyhow::Error>) -> Self {
        Self {
            error: Some(e.into()),
            code: 101,
        }
    }
}

macro_rules! process_error_from {
    ($from:ty) => {
        impl From<$from> for CliError {
            fn from(error: $from) -> Self {
                Self::message(error)
            }
        }
    };
}

process_error_from!(anyhow::Error);
process_error_from!(std::io::Error);
process_error_from!(semver::Error);
process_error_from!(ignore::Error);
process_error_from!(crates_index::Error);
process_error_from!(cargo_metadata::Error);
process_error_from!(toml::ser::Error);
process_error_from!(toml_edit::ser::Error);

impl From<i32> for CliError {
    fn from(code: i32) -> Self {
        Self::silent(code)
    }
}

impl std::fmt::Display for CliError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if let Some(error) = self.error.as_ref() {
            error.fmt(f)
        } else {
            Ok(())
        }
    }
}

/// Report any error message and exit.
pub fn exit(result: Result<(), CliError>) -> ! {
    let code = report(result);
    std::process::exit(code)
}

/// Report, delegating exiting to the caller.
pub fn report(result: Result<(), CliError>) -> i32 {
    match result {
        Ok(()) => 0,
        Err(err) => {
            if let Some(error) = err.error {
                // At this point, we might be exiting due to a broken pipe, just do our best and
                // move on.
                let _ = crate::ops::shell::error(error);
            }
            err.code
        }
    }
}

pub type CargoResult<T> = anyhow::Result<T>;