Debian Codegraph @

rust/hg-pyo3/src/revlog/index.rs

raw
1 // revlog/index.rs
2 //
3 // Copyright 2019-2020 Georges Racinet <[email protected]>
4 // 2020-2024 Raphaël Gomès <[email protected]>
5 // 2024 Georges Racinet <[email protected]>
6 //
7 // This software may be used and distributed according to the terms of the
8 // GNU General Public License version 2 or any later version.
9 //! Utilities for dealing with the index at the Python boundary
10 use hg::revlog::index::Index;
11 use hg::revlog::index::RevisionDataParams;
12 use hg::revlog::Node;
13 use hg::revlog::Revision;
14 use hg::revlog::RevlogIndex;
15 use hg::BaseRevision;
16 use hg::Graph;
17 use pyo3::prelude::*;
18 use pyo3::types::PyBytes;
19 use pyo3::types::PyTuple;
20 use vcsgraph::graph::Graph as VCSGraph;
21
22 #[derive(derive_more::From, Clone)]
23 pub struct PySharedIndex {
24 /// The underlying hg-core index
25 inner: &'static Index,
26 }
27
28 impl PySharedIndex {
29 /// Return a reference to the inner index, bound by `self`
30 pub fn inner(&self) -> &Index {
31 self.inner
32 }
33
34 /// Return an unsafe "faked" `'static` reference to the inner index, for
35 /// the purposes of Python <-> Rust memory sharing.
36 pub unsafe fn static_inner(&self) -> &'static Index {
37 self.inner
38 }
39 }
40
41 impl RevlogIndex for PySharedIndex {
42 fn len(&self) -> usize {
43 self.inner.len()
44 }
45 fn node(&self, rev: Revision) -> &Node {
46 self.inner.node(rev)
47 }
48 }
49
50 impl Graph for PySharedIndex {
51 #[inline(always)]
52 fn parents(&self, rev: Revision) -> Result<[Revision; 2], hg::GraphError> {
53 self.inner.parents(rev)
54 }
55 }
56
57 impl VCSGraph for PySharedIndex {
58 #[inline(always)]
59 fn parents(
60 &self,
61 rev: BaseRevision,
62 ) -> Result<vcsgraph::graph::Parents, vcsgraph::graph::GraphReadError> {
63 // FIXME This trait should be reworked to decide between Revision
64 // and UncheckedRevision, get better errors names, etc.
65 match Graph::parents(self, Revision(rev)) {
66 Ok(parents) => {
67 Ok(vcsgraph::graph::Parents([parents[0].0, parents[1].0]))
68 }
69 Err(hg::GraphError::ParentOutOfRange(rev)) => {
70 Err(vcsgraph::graph::GraphReadError::KeyedInvalidKey(rev.0))
71 }
72 Err(hg::GraphError::ParentOutOfOrder(rev)) => {
73 Err(vcsgraph::graph::GraphReadError::KeyedInvalidKey(rev.0))
74 }
75 }
76 }
77 }
78
79 pub fn py_tuple_to_revision_data_params(
80 tuple: &Bound<'_, PyTuple>,
81 ) -> PyResult<RevisionDataParams> {
82 // no need to check length: in PyO3 tup.get_item() does return
83 // proper errors
84 let offset_or_flags: u64 = tuple.get_item(0)?.extract()?;
85 let node_id = tuple
86 .get_item(7)?
87 .cast::<PyBytes>()?
88 .as_bytes()
89 .try_into()
90 .expect("nodeid should be set");
91 let flags = (offset_or_flags & 0xFFFF) as u16;
92 let data_offset = offset_or_flags >> 16;
93 Ok(RevisionDataParams {
94 flags,
95 data_offset,
96 data_compressed_length: tuple.get_item(1)?.extract()?,
97 data_uncompressed_length: tuple.get_item(2)?.extract()?,
98 data_delta_base: tuple.get_item(3)?.extract()?,
99 link_rev: tuple.get_item(4)?.extract()?,
100 parent_rev_1: tuple.get_item(5)?.extract()?,
101 parent_rev_2: tuple.get_item(6)?.extract()?,
102 node_id,
103 ..Default::default()
104 })
105 }
106
107 pub fn revision_data_params_to_py_tuple(
108 py: Python<'_>,
109 params: RevisionDataParams,
110 ) -> PyResult<Bound<'_, PyTuple>> {
111 PyTuple::new(
112 py,
113 &[
114 params.data_offset.into_pyobject(py)?.into_any(),
115 params.data_compressed_length.into_pyobject(py)?.into_any(),
116 params.data_uncompressed_length.into_pyobject(py)?.into_any(),
117 params.data_delta_base.into_pyobject(py)?.into_any(),
118 params.link_rev.into_pyobject(py)?.into_any(),
119 params.parent_rev_1.into_pyobject(py)?.into_any(),
120 params.parent_rev_2.into_pyobject(py)?.into_any(),
121 PyBytes::new(py, &params.node_id).into_any().into_any(),
122 params._sidedata_offset.into_pyobject(py)?.into_any(),
123 params._sidedata_compressed_length.into_pyobject(py)?.into_any(),
124 params.data_compression_mode.into_pyobject(py)?.into_any(),
125 params._sidedata_compression_mode.into_pyobject(py)?.into_any(),
126 params._rank.into_pyobject(py)?.into_any(),
127 ],
128 )
129 }