rust/hg-pyo3/src/dirstate/dirstate_map.rs
changeset 52857 e7b825893e1b
parent 52854 4e5efcaa7e60
child 52859 9f083ff3c96c
--- a/rust/hg-pyo3/src/dirstate/dirstate_map.rs	Thu Feb 06 11:40:38 2025 +0100
+++ b/rust/hg-pyo3/src/dirstate/dirstate_map.rs	Wed Jan 29 14:05:26 2025 +0100
@@ -9,8 +9,77 @@
 //! `hg-core` package.
 
 use pyo3::prelude::*;
+use pyo3::types::{PyBytes, PyBytesMethods, PyTuple};
+use pyo3_sharedref::PyShareable;
 
-use hg::dirstate::dirstate_map::DirstateIdentity as CoreDirstateIdentity;
+use hg::dirstate::{
+    dirstate_map::DirstateIdentity as CoreDirstateIdentity,
+    owning::OwningDirstateMap,
+};
+
+use crate::{exceptions::dirstate_error, node::PyNode, utils::PyBytesDeref};
+
+#[pyclass]
+pub struct DirstateMap {
+    #[allow(dead_code)]
+    inner: PyShareable<OwningDirstateMap>,
+}
+
+#[pymethods]
+impl DirstateMap {
+    #[staticmethod]
+    #[pyo3(signature = (on_disk, identity))]
+    /// Returns a `(dirstate_map, parents)` tuple
+    ///
+    /// The Python call site is using the positional argument style, hence
+    /// despite the fact that `identity` can be `None`, we specify the
+    /// matching signature.
+    fn new_v1(
+        py: Python,
+        on_disk: Py<PyBytes>,
+        identity: Option<&Bound<'_, DirstateIdentity>>,
+    ) -> PyResult<Py<PyTuple>> {
+        let on_disk = PyBytesDeref::new(py, on_disk);
+        let (map, parents) = OwningDirstateMap::new_v1(
+            on_disk,
+            identity.map(|i| i.borrow().inner),
+        )
+        .map_err(dirstate_error)?;
+        let map = Self { inner: map.into() };
+        let parents = (PyNode(parents.p1), PyNode(parents.p2));
+        Ok((map, parents).into_pyobject(py)?.into())
+    }
+
+    #[staticmethod]
+    #[pyo3(signature = (on_disk, data_size, tree_metadata, uuid, identity))]
+    fn new_v2(
+        py: Python,
+        on_disk: Py<PyBytes>,
+        data_size: usize,
+        tree_metadata: &Bound<'_, PyBytes>,
+        uuid: &Bound<'_, PyBytes>,
+        identity: Option<&Bound<'_, DirstateIdentity>>,
+    ) -> PyResult<Self> {
+        Ok(Self {
+            inner: OwningDirstateMap::new_v2(
+                PyBytesDeref::new(py, on_disk),
+                data_size,
+                tree_metadata.as_bytes(),
+                uuid.as_bytes().to_owned(),
+                identity.map(|i| i.borrow().inner),
+            )
+            .map_err(dirstate_error)?
+            .into(),
+        })
+    }
+
+    #[staticmethod]
+    fn new_empty() -> PyResult<Self> {
+        Ok(Self {
+            inner: OwningDirstateMap::new_empty(vec![], None).into(),
+        })
+    }
+}
 
 #[pyclass]
 pub struct DirstateIdentity {