Debian Codegraph @

rust/hg-pyo3/src/revision.rs

raw
1 use hg::revlog::RevlogIndex;
2 use hg::BaseRevision;
3 use hg::Revision;
4 use hg::UncheckedRevision;
5 use pyo3::prelude::*;
6 use pyo3::types::PyList;
7 use pyo3::types::PySet;
8
9 use crate::exceptions::rev_not_in_index;
10 use crate::exceptions::GraphError;
11 use crate::utils::proxy_index_extract;
12
13 /// Revision as exposed to/from the Python layer.
14 ///
15 /// We need this indirection because of the orphan rule, meaning we can't
16 /// implement a foreign trait (like [`pyo3::ToPyObject`])
17 /// for a foreign type (like [`hg::UncheckedRevision`]).
18 ///
19 /// This also acts as a deterrent against blindly trusting Python to send
20 /// us valid revision numbers.
21 #[derive(
22 Debug,
23 Copy,
24 Clone,
25 PartialEq,
26 Eq,
27 PartialOrd,
28 Ord,
29 Hash,
30 derive_more::From,
31 IntoPyObject,
32 FromPyObject,
33 )]
34 pub struct PyRevision(pub BaseRevision);
35
36 impl From<Revision> for PyRevision {
37 fn from(r: Revision) -> Self {
38 PyRevision(r.0)
39 }
40 }
41
42 impl From<PyRevision> for UncheckedRevision {
43 fn from(val: PyRevision) -> Self {
44 val.0.into()
45 }
46 }
47
48 pub fn check_revision(
49 index: &impl RevlogIndex,
50 rev: impl Into<UncheckedRevision>,
51 ) -> PyResult<Revision> {
52 let rev = rev.into();
53 index.check_revision(rev).ok_or_else(|| rev_not_in_index(rev))
54 }
55
56 /// Utility function to convert a Python iterable into various collections
57 ///
58 /// We need this in particular
59 /// - because of the checked upgrade from [`PyRevision`] to [`Revision`].
60 /// - to feed to various methods of inner objects with `impl
61 /// IntoIterator<Item=Revision>` arguments, because a `PyErr` can arise at
62 /// each step of iteration, whereas these methods expect iterables over
63 /// `Revision`, not over some `Result<Revision, PyErr>`
64 pub fn rev_pyiter_collect<C, I>(
65 revs: &Bound<'_, PyAny>,
66 index: &I,
67 ) -> PyResult<C>
68 where
69 C: FromIterator<Revision>,
70 I: RevlogIndex,
71 {
72 rev_pyiter_collect_or_else(revs, index, |r| {
73 PyErr::new::<GraphError, _>(("InvalidRevision", r.0))
74 })
75 }
76
77 pub fn rev_pyiter_collect_with_py_index<C>(
78 revs: &Bound<'_, PyAny>,
79 proxy_index: &Bound<'_, PyAny>,
80 ) -> PyResult<C>
81 where
82 C: FromIterator<Revision>,
83 {
84 // Safety: we don't leak the "faked" reference out of `UnsafePyLeaked`
85 let borrowed_idx = unsafe { proxy_index_extract(proxy_index)? };
86 rev_pyiter_collect(revs, borrowed_idx)
87 }
88
89 /// Same as [`rev_pyiter_collect`], giving control on returned errors
90 pub fn rev_pyiter_collect_or_else<C, I>(
91 revs: &Bound<'_, PyAny>,
92 index: &I,
93 invalid_rev_error: impl FnOnce(PyRevision) -> PyErr + Copy,
94 ) -> PyResult<C>
95 where
96 C: FromIterator<Revision>,
97 I: RevlogIndex,
98 {
99 revs.try_iter()?
100 .map(|r| {
101 r.and_then(|o| match o.extract::<PyRevision>() {
102 Ok(r) => index
103 .check_revision(UncheckedRevision(r.0))
104 .ok_or_else(|| invalid_rev_error(r)),
105 Err(e) => Err(e),
106 })
107 })
108 .collect()
109 }
110
111 pub fn revs_py_list<U>(
112 py: Python<'_>,
113 revs: impl IntoIterator<Item = Revision, IntoIter = U>,
114 ) -> PyResult<Py<PyList>>
115 where
116 U: ExactSizeIterator<Item = Revision>,
117 {
118 Ok(PyList::new(py, revs.into_iter().map(PyRevision::from))?.unbind())
119 }
120
121 pub fn revs_py_set<U>(
122 py: Python<'_>,
123 revs: impl IntoIterator<Item = Revision, IntoIter = U>,
124 ) -> PyResult<Py<PySet>>
125 where
126 U: ExactSizeIterator<Item = Revision>,
127 {
128 Ok(PySet::new(py, revs.into_iter().map(PyRevision::from))?.unbind())
129 }