1use diffy::{ApplyError, ParsePatchError};
2use std::fmt;
3use std::io::Error as IoError;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum Error {
8 #[error(transparent)]
9 Io(#[from] IoError),
10
11 #[error(transparent)]
12 Subprocess(#[from] SubprocessError),
13
14 #[error(transparent)]
15 ParsePatch(#[from] ParsePatchError),
16
17 #[error(transparent)]
18 ApplyPatch(#[from] ApplyError),
19}
20
21#[derive(Error, Debug)]
22pub struct SubprocessError {
23 pub(crate) command: String,
24 pub(crate) code: Option<i32>,
25 pub(crate) stderr: Option<String>,
26}
27
28impl SubprocessError {
29 pub fn command(&self) -> &str {
30 &self.command
31 }
32
33 pub fn code(&self) -> Option<i32> {
34 self.code
35 }
36
37 pub fn stderr(&self) -> Option<&str> {
38 self.stderr.as_deref()
39 }
40}
41
42impl fmt::Display for SubprocessError {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 write!(f, "subprocess `{}` failed", self.command)?;
45
46 if let Some(code) = self.code {
47 write!(f, " with code {code}")?;
48 }
49
50 if f.alternate() {
51 if let Some(ref stderr) = self.stderr {
52 writeln!(f, ":\n{stderr}")?;
53 }
54 }
55
56 Ok(())
57 }
58}