changeset 52790:1b9907575768

rust-pyo3-revlog: helpers for revision checks The new `check_revision` function will take care of the annoying input and errors conversions. The conversion from `PyRevision` to `UncheckedRevision` will also be handy.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Tue, 24 Dec 2024 15:08:22 +0100
parents 34f44aa5e844
children 0ac956db7ea7
files rust/hg-pyo3/src/exceptions.rs rust/hg-pyo3/src/revision.rs
diffstat 2 files changed, 24 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-pyo3/src/exceptions.rs	Wed Dec 25 17:17:47 2024 +0100
+++ b/rust/hg-pyo3/src/exceptions.rs	Tue Dec 24 15:08:22 2024 +0100
@@ -3,6 +3,7 @@
 use pyo3::{create_exception, PyErr};
 
 use hg::revlog::nodemap::NodeMapError;
+use hg::UncheckedRevision;
 
 use crate::revision::PyRevision;
 
@@ -58,6 +59,11 @@
     mercurial_py_errors::RevlogError::new_err((None::<String>,))
 }
 
+#[allow(dead_code)]
+pub fn rev_not_in_index(rev: UncheckedRevision) -> PyErr {
+    PyValueError::new_err(format!("revlog index out of range: {}", rev))
+}
+
 pub fn nodemap_error(err: NodeMapError) -> PyErr {
     match err {
         NodeMapError::MultipleResults => {
--- a/rust/hg-pyo3/src/revision.rs	Wed Dec 25 17:17:47 2024 +0100
+++ b/rust/hg-pyo3/src/revision.rs	Tue Dec 24 15:08:22 2024 +0100
@@ -4,7 +4,7 @@
 use hg::{BaseRevision, Revision, UncheckedRevision};
 
 use crate::convert_cpython::proxy_index_extract;
-use crate::exceptions::GraphError;
+use crate::exceptions::{rev_not_in_index, GraphError};
 
 /// Revision as exposed to/from the Python layer.
 ///
@@ -35,6 +35,23 @@
     }
 }
 
+impl From<PyRevision> for UncheckedRevision {
+    fn from(val: PyRevision) -> Self {
+        val.0.into()
+    }
+}
+
+#[allow(dead_code)]
+pub fn check_revision(
+    index: &impl RevlogIndex,
+    rev: impl Into<UncheckedRevision>,
+) -> PyResult<Revision> {
+    let rev = rev.into();
+    index
+        .check_revision(rev)
+        .ok_or_else(|| rev_not_in_index(rev))
+}
+
 /// Utility function to convert a Python iterable into various collections
 ///
 /// We need this in particular