changeset 52874:09eb477eec65

rust-pyo3-path: more conversions to Python The `PyHgPathDirstateV2Result` new type is unfortunately heavily named, but it should allow for very straightforward conversions of collections.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Thu, 06 Feb 2025 13:57:51 +0100
parents c5773445d350
children ab6198160960
files rust/hg-pyo3/src/path.rs
diffstat 1 files changed, 42 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-pyo3/src/path.rs	Wed Jan 29 19:13:46 2025 +0100
+++ b/rust/hg-pyo3/src/path.rs	Thu Feb 06 13:57:51 2025 +0100
@@ -13,8 +13,11 @@
 
 use std::convert::Infallible;
 
+use hg::dirstate::on_disk::DirstateV2ParseError;
 use hg::utils::hg_path::{HgPath, HgPathBuf};
 
+use crate::exceptions::dirstate_v2_error;
+
 #[derive(Eq, Ord, PartialEq, PartialOrd, Hash, derive_more::From)]
 pub struct PyHgPathRef<'a>(pub &'a HgPath);
 
@@ -31,6 +34,45 @@
     }
 }
 
+#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, derive_more::From)]
+pub struct PyHgPathBuf(pub HgPathBuf);
+
+// This is for now equivalent to taking a ref as `HgPath` and using
+// `HgPathRef`. One day, perhaps, this variant for owned data could be
+// implemented without allocation.
+impl<'py> IntoPyObject<'py> for PyHgPathBuf {
+    type Target = PyBytes;
+    type Output = Bound<'py, Self::Target>;
+    type Error = Infallible;
+
+    fn into_pyobject(
+        self,
+        py: Python<'py>,
+    ) -> Result<Self::Output, Self::Error> {
+        Ok(PyBytes::new(py, self.0.as_bytes()))
+    }
+}
+
+pub struct PyHgPathDirstateV2Result<'a>(
+    pub Result<&'a HgPath, DirstateV2ParseError>,
+);
+
+impl<'py> IntoPyObject<'py> for PyHgPathDirstateV2Result<'_> {
+    type Target = PyBytes;
+    type Output = Bound<'py, Self::Target>;
+    type Error = PyErr;
+
+    fn into_pyobject(
+        self,
+        py: Python<'py>,
+    ) -> Result<Self::Output, Self::Error> {
+        Ok(PyBytes::new(
+            py,
+            self.0.map_err(dirstate_v2_error)?.as_bytes(),
+        ))
+    }
+}
+
 #[allow(dead_code)]
 pub fn paths_py_list<I, U>(
     py: Python<'_>,