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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::fmt::Debug;

pub enum Error {
    NoMergeCommit {
        r#for: String,
    },
    PicoArgsError(pico_args::Error),
    FindCommitError {
        sha: String,
        source: git2::Error,
    },
    GraphDescendantOfError {
        commit: String,
        ancestory: String,
        source: git2::Error,
    },
    RevParseError {
        sha: String,
        source: git2::Error,
    },
    ExpectedCommitError {
        sha: String,
    },
    FindRemoteError {
        remote: String,
        source: git2::Error,
    },
    Git2Error(git2::Error),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::NoMergeCommit { r#for } => {
                write!(f, "No merge commit for {} found", r#for)
            }
            Error::PicoArgsError(e) => write!(f, "{}", e),
            Error::FindCommitError { sha, source: _ } => {
                write!(f, "Failed to find commit {}", sha)
            }
            Error::GraphDescendantOfError {
                commit,
                ancestory,
                source: _,
            } => {
                write!(
                    f,
                    "Failed to determine of {} is a descendant of {}",
                    commit, ancestory,
                )
            }
            Error::RevParseError { sha, source: _ } => {
                write!(f, "Failed to parse revision {}", sha)
            }
            Error::ExpectedCommitError { sha } => {
                write!(f, "Expected {} to be a commit", sha)
            }
            Error::FindRemoteError { remote, source: _ } => {
                write!(f, "Failed to find remote {}", remote)
            }
            Error::Git2Error(e) => write!(f, "{}", e),
        }
    }
}

impl Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "{}", self)?;
        writeln!(f)?;

        let mut maybe_e = std::error::Error::source(self);
        while let Some(e) = maybe_e {
            writeln!(f, "{}", e)?;
            maybe_e = std::error::Error::source(e);
        }
        Ok(())
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::NoMergeCommit { r#for: _ } => None,
            Error::PicoArgsError(e) => Some(e),
            Error::FindCommitError { sha: _, source } => Some(source),
            Error::GraphDescendantOfError {
                commit: _,
                ancestory: _,
                source,
            } => Some(source),
            Error::RevParseError { sha: _, source } => Some(source),
            Error::ExpectedCommitError { sha: _ } => None,
            Error::FindRemoteError { remote: _, source } => Some(source),
            Error::Git2Error(e) => e.source(),
        }
    }

    fn cause(&self) -> Option<&dyn std::error::Error> {
        self.source()
    }
}

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

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

pub type Result<T> = std::result::Result<T, Error>;