girt-git 2.3.0

Git module for git-interactive-rebase-tool
Documentation
//! 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 enum RepositoryLoadKind {
	/// Repository was loaded from the path provided through an environment variable
	Environment,
	/// Repository was loaded from a direct path
	Path,
}

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

/// Git errors
#[derive(Error, Debug, PartialEq)]
#[non_exhaustive]
pub 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
	#[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");
	}
}