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

#[derive(Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Io(#[from] IoError),

    #[error(transparent)]
    Subprocess(#[from] SubprocessError),

    #[error(transparent)]
    ParsePatch(#[from] ParsePatchError),

    #[error(transparent)]
    ApplyPatch(#[from] ApplyError),
}

#[derive(Error, 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 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(())
    }
}