use hg::revlog::RevlogIndex; use hg::BaseRevision; use hg::Revision; use hg::UncheckedRevision; use pyo3::prelude::*; use pyo3::types::PyList; use pyo3::types::PySet; use crate::exceptions::rev_not_in_index; use crate::exceptions::GraphError; use crate::utils::proxy_index_extract; /// Revision as exposed to/from the Python layer. /// /// We need this indirection because of the orphan rule, meaning we can't /// implement a foreign trait (like [`pyo3::ToPyObject`]) /// for a foreign type (like [`hg::UncheckedRevision`]). /// /// This also acts as a deterrent against blindly trusting Python to send /// us valid revision numbers. #[derive( Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, derive_more::From, IntoPyObject, FromPyObject, )] pub struct PyRevision(pub BaseRevision); impl From for PyRevision { fn from(r: Revision) -> Self { PyRevision(r.0) } } impl From for UncheckedRevision { fn from(val: PyRevision) -> Self { val.0.into() } } pub fn check_revision( index: &impl RevlogIndex, rev: impl Into, ) -> PyResult { let rev = rev.into(); index.check_revision(rev).ok_or_else(|| rev_not_in_index(rev)) } /// Utility function to convert a Python iterable into various collections /// /// We need this in particular /// - because of the checked upgrade from [`PyRevision`] to [`Revision`]. /// - to feed to various methods of inner objects with `impl /// IntoIterator` arguments, because a `PyErr` can arise at /// each step of iteration, whereas these methods expect iterables over /// `Revision`, not over some `Result` pub fn rev_pyiter_collect( revs: &Bound<'_, PyAny>, index: &I, ) -> PyResult where C: FromIterator, I: RevlogIndex, { rev_pyiter_collect_or_else(revs, index, |r| { PyErr::new::(("InvalidRevision", r.0)) }) } pub fn rev_pyiter_collect_with_py_index( revs: &Bound<'_, PyAny>, proxy_index: &Bound<'_, PyAny>, ) -> PyResult where C: FromIterator, { // Safety: we don't leak the "faked" reference out of `UnsafePyLeaked` let borrowed_idx = unsafe { proxy_index_extract(proxy_index)? }; rev_pyiter_collect(revs, borrowed_idx) } /// Same as [`rev_pyiter_collect`], giving control on returned errors pub fn rev_pyiter_collect_or_else( revs: &Bound<'_, PyAny>, index: &I, invalid_rev_error: impl FnOnce(PyRevision) -> PyErr + Copy, ) -> PyResult where C: FromIterator, I: RevlogIndex, { revs.try_iter()? .map(|r| { r.and_then(|o| match o.extract::() { Ok(r) => index .check_revision(UncheckedRevision(r.0)) .ok_or_else(|| invalid_rev_error(r)), Err(e) => Err(e), }) }) .collect() } pub fn revs_py_list( py: Python<'_>, revs: impl IntoIterator, ) -> PyResult> where U: ExactSizeIterator, { Ok(PyList::new(py, revs.into_iter().map(PyRevision::from))?.unbind()) } pub fn revs_py_set( py: Python<'_>, revs: impl IntoIterator, ) -> PyResult> where U: ExactSizeIterator, { Ok(PySet::new(py, revs.into_iter().map(PyRevision::from))?.unbind()) }