Debian Codegraph @

rust/hg-pyo3/src/dagops.rs

raw
1 // dagops.rs
2 //
3 // Copyright 2024 Georges Racinet <[email protected]>
4 //
5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version.
7
8 //! Bindings for the `hg::dagops` module provided by the
9 //! `hg-core` package.
10 //!
11 //! From Python, this will be seen as `mercurial.pyo3_rustext.dagop`
12 use std::collections::HashSet;
13
14 use hg::dagops;
15 use hg::Revision;
16 use pyo3::prelude::*;
17
18 use crate::exceptions::GraphError;
19 use crate::revision::rev_pyiter_collect;
20 use crate::revision::PyRevision;
21 use crate::utils::new_submodule;
22 use crate::utils::proxy_index_extract;
23
24 /// Using the the `index_proxy`, return heads out of any Python iterable of
25 /// Revisions
26 ///
27 /// This is the Rust counterpart for `mercurial.dagop.headrevs`
28 #[pyfunction]
29 pub fn headrevs(
30 index_proxy: &Bound<'_, PyAny>,
31 revs: &Bound<'_, PyAny>,
32 ) -> PyResult<HashSet<PyRevision>> {
33 // Safety: we don't leak the "faked" reference out of `SharedByPyObject`
34 let index = unsafe { proxy_index_extract(index_proxy)? };
35 let mut as_set: HashSet<Revision> = rev_pyiter_collect(revs, index)?;
36 dagops::retain_heads(index, &mut as_set).map_err(GraphError::from_hg)?;
37 Ok(as_set.into_iter().map(Into::into).collect())
38 }
39
40 /// Computes the rank, i.e. the number of ancestors including itself,
41 /// of a node represented by its parents.
42 ///
43 /// Currently, the pure Rust index supports only the REVLOGV1 format, hence
44 /// the only possible return value is that the rank is unknown.
45 ///
46 /// References:
47 /// - C implementation, function `index_fast_rank()`.
48 /// - `impl vcsgraph::graph::RankedGraph for Index` in `crate::cindex`.
49 #[pyfunction]
50 pub fn rank(
51 _index: &Bound<'_, PyAny>,
52 _p1r: PyRevision,
53 _p2r: PyRevision,
54 ) -> PyResult<()> {
55 Err(GraphError::from_vcsgraph(
56 vcsgraph::graph::GraphReadError::InconsistentGraphData,
57 ))
58 }
59
60 pub fn init_module<'py>(
61 py: Python<'py>,
62 package: &str,
63 ) -> PyResult<Bound<'py, PyModule>> {
64 let m = new_submodule(py, package, "dagop")?;
65 m.add_function(wrap_pyfunction!(headrevs, &m)?)?;
66 m.add_function(wrap_pyfunction!(rank, &m)?)?;
67 Ok(m)
68 }