changeset 52879:aa2cfeed65c9

rust-pyo3-dirstate: Dirs definition and constructor
author Georges Racinet <georges.racinet@cloudcrane.io>
date Tue, 04 Feb 2025 19:37:22 +0100
parents c917656a259d
children 8e94b32de84b
files rust/hg-pyo3/src/dirstate.rs rust/hg-pyo3/src/dirstate/dirs_multiset.rs
diffstat 2 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-pyo3/src/dirstate.rs	Tue Feb 04 13:36:42 2025 +0100
+++ b/rust/hg-pyo3/src/dirstate.rs	Tue Feb 04 19:37:22 2025 +0100
@@ -21,6 +21,8 @@
 };
 mod copy_map;
 use copy_map::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator};
+mod dirs_multiset;
+use dirs_multiset::Dirs;
 
 pub fn init_module<'py>(
     py: Python<'py>,
@@ -37,5 +39,6 @@
     m.add_class::<CopyMap>()?;
     m.add_class::<CopyMapKeysIterator>()?;
     m.add_class::<CopyMapItemsIterator>()?;
+    m.add_class::<Dirs>()?;
     Ok(m)
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/hg-pyo3/src/dirstate/dirs_multiset.rs	Tue Feb 04 19:37:22 2025 +0100
@@ -0,0 +1,46 @@
+// copy_map.rs
+//
+// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
+//           2025 Georges Racinet <georges.racinet@cloudcrane.io>
+//
+// This software may be used and distributed according to the terms of the
+// GNU General Public License version 2 or any later version.
+//! Bindings for `hg::dirstate::dirs_multiset` file provided by the
+//! `hg-core` package.
+use pyo3::exceptions::PyTypeError;
+use pyo3::prelude::*;
+use pyo3::types::PyDict;
+use pyo3_sharedref::PyShareable;
+
+use hg::{dirstate::dirs_multiset::DirsMultiset, utils::hg_path::HgPathBuf};
+
+use crate::exceptions::to_string_value_error;
+
+#[pyclass(mapping)]
+#[allow(dead_code)]
+pub struct Dirs {
+    pub(super) inner: PyShareable<DirsMultiset>,
+}
+
+#[pymethods]
+impl Dirs {
+    #[new]
+    fn new(map: &Bound<'_, PyAny>) -> PyResult<Self> {
+        if map.downcast::<PyDict>().is_ok() {
+            return Err(PyTypeError::new_err(
+                "pathutil.dirs() with a dict should only be used by the \
+                 Python dirstatemap and should not be used \
+                 when Rust is enabled",
+            ));
+        }
+        let map: Result<Vec<_>, PyErr> = map
+            .try_iter()?
+            .map(|o| Ok(HgPathBuf::from_bytes(o?.extract()?)))
+            .collect();
+        Ok(Self {
+            inner: DirsMultiset::from_manifest(&map?)
+                .map_err(to_string_value_error)?
+                .into(),
+        })
+    }
+}