hg-cpython: rev_pyiter_collect_or_else
authorGeorges Racinet <georges.racinet@octobus.net>
Thu, 02 Nov 2023 12:17:06 +0100
changeset 51221 5a7d5fd6808c
parent 51220 c817d9f626d3
child 51222 fc05dd74e907
hg-cpython: rev_pyiter_collect_or_else It will be useful to give callers the control on the generated errors
rust/hg-cpython/src/conversion.rs
--- 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),
             })
         })