summaryrefslogtreecommitdiffstats
path: root/src/git/errors.rs
blob: df789a7c64686afd83f6562e218a9dbcf36fcb2b (plain)
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
//! Git Interactive Rebase Tool - Git crate errors
//!
//! # Description
//! This module contains error types used in the Git crate.

use std::fmt::{Display, Formatter};

use thiserror::Error;

/// The kind of repository load kind.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub(crate) enum RepositoryLoadKind {
	/// Repository was loaded from the path provided through an environment variable
	Environment,
	#[cfg(test)]
	/// Repository was loaded from a direct path
	Path,
}

impl Display for RepositoryLoadKind {
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
		match *self {
			Self::Environment => write!(f, "environment"),
			#[cfg(test)]
			Self::Path => write!(f, "path"),
		}
	}
}

/// Git errors
#[derive(Error, Debug, PartialEq)]
#[non_exhaustive]
#[allow(clippy::enum_variant_names)]
pub(crate) enum GitError {
	/// The repository could not be loaded
	#[error("Could not open repository from {kind}")]
	RepositoryLoad {
		/// The kind of load error.
		kind: RepositoryLoadKind,
		/// The internal cause of the load error.
		#[source]
		cause: git2::Error,
	},
	/// The configuration could not be loaded
	#[error("Could not load configuration")]
	ConfigLoad {
		/// The internal cause of the load error.
		#[source]
		cause: git2::Error,
	},
	/// The configuration could not be loaded
	#[cfg(test)]
	#[error("Could not load configuration")]
	ReferenceNotFound {
		/// The internal cause of the load error.
		#[source]
		cause: git2::Error,
	},
	/// The commit could not be loaded
	#[error("Could not load commit")]
	CommitLoad {
		/// The internal cause of the load error.
		#[source]
		cause: git2::Error,
	},
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn repository_load_kind_display_environment() {
		assert_eq!(format!("{}", RepositoryLoadKind::Environment), "environment");
	}

	#[test]
	fn repository_load_kind_display_path() {
		assert_eq!(format!("{}", RepositoryLoadKind::Path), "path");
	}
}