From 813a3bea88cdbe1fd9b0a8070efeee2a44f7823e Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 26 Jan 2022 20:07:03 +0800 Subject: [PATCH] =?UTF-8?q?feat!:=20Let=20'easy::Object::try=5Finto=5F?= =?UTF-8?q?=E2=80=A6()`=20return=20`try=5Finto::Error`.=20(#298)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That way, the typical usage of `try_into_commit()?` will not result in a strange error about `Object` not being convertible into some error. We think having a real error there is the least surprising. --- git-repository/src/easy/object/commit.rs | 6 ++--- git-repository/src/easy/object/mod.rs | 28 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/git-repository/src/easy/object/commit.rs b/git-repository/src/easy/object/commit.rs index ebd8a06432c..233fedde9e7 100644 --- a/git-repository/src/easy/object/commit.rs +++ b/git-repository/src/easy/object/commit.rs @@ -21,6 +21,7 @@ mod error { pub use error::Error; use crate::bstr::BStr; +use crate::easy; impl<'repo> Commit<'repo> { /// Parse the commits message into a [`MessageRef`][git_object::commit::MessageRef], after decoding the entire commit object. @@ -46,10 +47,7 @@ impl<'repo> Commit<'repo> { let tree_id = self.tree_id().ok_or(Error::Decode)?; match self.handle.find_object(tree_id)?.try_into_tree() { Ok(tree) => Ok(tree), - Err(obj) => Err(Error::ObjectKind { - actual: obj.kind, - expected: git_object::Kind::Tree, - }), + Err(easy::object::try_into::Error { actual, expected, .. }) => Err(Error::ObjectKind { actual, expected }), } } diff --git a/git-repository/src/easy/object/mod.rs b/git-repository/src/easy/object/mod.rs index fa3057998bd..95167a93d8b 100644 --- a/git-repository/src/easy/object/mod.rs +++ b/git-repository/src/easy/object/mod.rs @@ -21,6 +21,18 @@ pub mod peel; /// pub mod tree; +/// +pub mod try_into { + #[derive(thiserror::Error, Debug)] + #[allow(missing_docs)] + #[error("Object named {id} was supposed to be of kind {expected}, but was kind {actual}.")] + pub struct Error { + pub actual: git_object::Kind, + pub expected: git_object::Kind, + pub id: git_hash::ObjectId, + } +} + impl DetachedObject { /// Infuse this owned object with an [`easy::Handle`]. pub fn attach(self, handle: &easy::Handle) -> Object<'_> { @@ -60,13 +72,21 @@ impl<'repo> Object<'repo> { } /// Transform this object into a commit, or return it as part of the `Err` if it is no commit. - pub fn try_into_commit(self) -> Result, Self> { - self.try_into() + pub fn try_into_commit(self) -> Result, try_into::Error> { + self.try_into().map_err(|this: Self| try_into::Error { + id: this.id, + actual: this.kind, + expected: git_object::Kind::Commit, + }) } /// Transform this object into a tree, or return it as part of the `Err` if it is no tree. - pub fn try_into_tree(self) -> Result, Self> { - self.try_into() + pub fn try_into_tree(self) -> Result, try_into::Error> { + self.try_into().map_err(|this: Self| try_into::Error { + id: this.id, + actual: this.kind, + expected: git_object::Kind::Tree, + }) } }