From cfbcc90c9346da58807e13a56edc82748d7e9a67 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 30 Oct 2023 17:23:53 +0900 Subject: [PATCH] git_backend: box GitBackendInit/LoadError up front These error enums will wrap gix error types, and will become bigger enough for clippy to complain. --- lib/src/git_backend.rs | 50 +++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 418471d27c..f195db86ca 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -54,12 +54,12 @@ pub enum GitBackendInitError { #[error("Failed to open git repository: {0}")] OpenRepository(#[source] git2::Error), #[error(transparent)] - Path(#[from] PathError), + Path(PathError), } -impl From for BackendInitError { - fn from(err: GitBackendInitError) -> Self { - BackendInitError(err.into()) +impl From> for BackendInitError { + fn from(err: Box) -> Self { + BackendInitError(err) } } @@ -68,12 +68,12 @@ pub enum GitBackendLoadError { #[error("Failed to open git repository: {0}")] OpenRepository(#[source] git2::Error), #[error(transparent)] - Path(#[from] PathError), + Path(PathError), } -impl From for BackendLoadError { - fn from(err: GitBackendLoadError) -> Self { - BackendLoadError(err.into()) +impl From> for BackendLoadError { + fn from(err: Box) -> Self { + BackendLoadError(err) } } @@ -120,13 +120,17 @@ impl GitBackend { } } - pub fn init_internal(store_path: &Path) -> Result { + pub fn init_internal(store_path: &Path) -> Result> { let git_repo = git2::Repository::init_bare(store_path.join("git")) .map_err(GitBackendInitError::InitRepository)?; let extra_path = store_path.join("extra"); - fs::create_dir(&extra_path).context(&extra_path)?; + fs::create_dir(&extra_path) + .context(&extra_path) + .map_err(GitBackendInitError::Path)?; let target_path = store_path.join("git_target"); - fs::write(&target_path, b"git").context(&target_path)?; + fs::write(&target_path, b"git") + .context(&target_path) + .map_err(GitBackendInitError::Path)?; let extra_metadata_store = TableStore::init(extra_path, HASH_LENGTH); Ok(GitBackend::new(git_repo, extra_metadata_store)) } @@ -134,9 +138,11 @@ impl GitBackend { pub fn init_external( store_path: &Path, git_repo_path: &Path, - ) -> Result { + ) -> Result> { let extra_path = store_path.join("extra"); - fs::create_dir(&extra_path).context(&extra_path)?; + fs::create_dir(&extra_path) + .context(&extra_path) + .map_err(GitBackendInitError::Path)?; let target_path = store_path.join("git_target"); if cfg!(windows) && git_repo_path.is_relative() { // When a repository is created in Windows, format the path with *forward @@ -149,10 +155,13 @@ impl GitBackend { .components() .map(|component| component.as_os_str().to_str().unwrap().to_owned()) .join("/"); - fs::write(&target_path, git_repo_path_string.as_bytes()).context(&target_path)?; + fs::write(&target_path, git_repo_path_string.as_bytes()) + .context(&target_path) + .map_err(GitBackendInitError::Path)?; } else { fs::write(&target_path, git_repo_path.to_str().unwrap().as_bytes()) - .context(&target_path)?; + .context(&target_path) + .map_err(GitBackendInitError::Path)?; }; let repo = git2::Repository::open(store_path.join(git_repo_path)) .map_err(GitBackendInitError::OpenRepository)?; @@ -160,12 +169,17 @@ impl GitBackend { Ok(GitBackend::new(repo, extra_metadata_store)) } - pub fn load(store_path: &Path) -> Result { + pub fn load(store_path: &Path) -> Result> { let git_repo_path = { let target_path = store_path.join("git_target"); - let git_repo_path_str = fs::read_to_string(&target_path).context(&target_path)?; + let git_repo_path_str = fs::read_to_string(&target_path) + .context(&target_path) + .map_err(GitBackendLoadError::Path)?; let git_repo_path = store_path.join(git_repo_path_str); - git_repo_path.canonicalize().context(&git_repo_path)? + git_repo_path + .canonicalize() + .context(&git_repo_path) + .map_err(GitBackendLoadError::Path)? }; let repo = git2::Repository::open(git_repo_path).map_err(GitBackendLoadError::OpenRepository)?;