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
use diffy::{ApplyError, ParsePatchError};
use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;

#[derive(Debug)]
pub enum Error {
    Io(IoError),
    Subprocess(SubprocessError),
    ParsePatch(ParsePatchError),
    ApplyPatch(ApplyError),
}

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

impl From<ParsePatchError> for Error {
    fn from(e: ParsePatchError) -> Self {
        Error::ParsePatch(e)
    }
}

impl From<ApplyError> for Error {
    fn from(e: ApplyError) -> Self {
        Error::ApplyPatch(e)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Io(e) => e.fmt(f),
            Error::Subprocess(e) => e.fmt(f),
            Error::ParsePatch(e) => e.fmt(f),
            Error::ApplyPatch(e) => e.fmt(f),
        }
    }
}

impl StdError for Error {}

#[derive(Debug)]
pub struct SubprocessError {
    pub(crate) command: String,
    pub(crate) code: Option<i32>,
    pub(crate) stderr: Option<String>,
}

impl SubprocessError {
    pub fn command(&self) -> &str {
        &self.command
    }

    pub fn code(&self) -> Option<i32> {
        self.code
    }

    pub fn stderr(&self) -> Option<&str> {
        self.stderr.as_deref()
    }
}

impl From<SubprocessError> for Error {
    fn from(e: SubprocessError) -> Self {
        Error::Subprocess(e)
    }
}

impl fmt::Display for SubprocessError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "subprocess `{}` failed", self.command)?;

        if let Some(code) = self.code {
            write!(f, " with code {code}")?;
        }

        if f.alternate() {
            if let Some(ref stderr) = self.stderr {
                writeln!(f, ":\n{stderr}")?;
            }
        }

        Ok(())
    }
}

impl StdError for SubprocessError {}