hg-cpython: rev_pyiter_collect_or_else
It will be useful to give callers the control on the generated errors
--- a/rust/hg-cpython/src/conversion.rs Mon Oct 30 11:54:42 2023 +0100
+++ b/rust/hg-cpython/src/conversion.rs Thu Nov 02 12:17:06 2023 +0100
@@ -28,17 +28,28 @@
C: FromIterator<Revision>,
I: RevlogIndex,
{
+ rev_pyiter_collect_or_else(py, revs, index, |r| {
+ PyErr::new::<GraphError, _>(py, ("InvalidRevision", r.0))
+ })
+}
+
+/// Same as [`rev_pyiter_collect`], giving control on returned errors
+pub fn rev_pyiter_collect_or_else<C, I>(
+ py: Python,
+ revs: &PyObject,
+ index: &I,
+ invalid_rev_error: impl FnOnce(PyRevision) -> PyErr + Copy,
+) -> PyResult<C>
+where
+ C: FromIterator<Revision>,
+ I: RevlogIndex,
+{
revs.iter(py)?
.map(|r| {
r.and_then(|o| match o.extract::<PyRevision>(py) {
Ok(r) => index
.check_revision(UncheckedRevision(r.0))
- .ok_or_else(|| {
- PyErr::new::<GraphError, _>(
- py,
- ("InvalidRevision", r.0),
- )
- }),
+ .ok_or_else(|| invalid_rev_error(r)),
Err(e) => Err(e),
})
})