I am working with some code that uses the libgit2 bindings for Rust to perform operations on a Git repository.
I have a section of code that transforms a "committish" reference (something that either is a commit or ultimately references a commit, like a tag) that currently looks like this:
let mut target_commit = target_object
.peel(git2::ObjectType::Commit)
.map_err(|_| anyhow!("Target `{commitish}` cannot be evaluated as a commit"))?
.into_commit()
.map_err(|_| anyhow!("Target `{commitish}` cannot be evaluated as a commit"))?;
I'd like to avoid having two identical calls to map_err
in that
chain. Based on the description of the and_then
combinator...
Calls op if the result is Ok, otherwise returns the Err value of self.
...I thought maybe this would work:
let mut target_commit = target_object
.peel(git2::ObjectType::Commit)
.and_then(|c| c.into_commit())
.map_err(|_| anyhow!("Target `{commitish}` cannot be evaluated as a commit"))?;
But that fails with:
106 | .and_then(|c| c.into_commit())
| ^^^^^^^^^^^^^^^ expected struct `git2::Error`, found struct `git2::Object`
What's the correct way to simplify this expression?