diff --git a/git-repository/src/easy/object/commit.rs b/git-repository/src/easy/object/commit.rs index ebd8a06432..233fedde9e 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 fa3057998b..95167a93d8 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, + }) } }