annotate rust/hg-pyo3/src/path.rs @ 52862:09eb477eec65

rust-pyo3-path: more conversions to Python The `PyHgPathDirstateV2Result` new type is unfortunately heavily named, but it should allow for very straightforward conversions of collections.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Thu, 06 Feb 2025 13:57:51 +0100
parents 9f083ff3c96c
children 38e16da74aea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52858
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
1 // path.rs
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
2 //
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
4 // 2025 Georges Racinet <georges.racinet@cloudcrane.io>
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
5 //
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
6 // This software may be used and distributed according to the terms of the
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
7 // GNU General Public License version 2 or any later version.
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
8 //! Utilities about `HgPath` and related objects provided by the `hg-core`
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
9 //! package.
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
10
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
11 use pyo3::prelude::*;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
12 use pyo3::types::{PyBytes, PyList};
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
13
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
14 use std::convert::Infallible;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
15
52862
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
16 use hg::dirstate::on_disk::DirstateV2ParseError;
52858
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
17 use hg::utils::hg_path::{HgPath, HgPathBuf};
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
18
52862
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
19 use crate::exceptions::dirstate_v2_error;
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
20
52858
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
21 #[derive(Eq, Ord, PartialEq, PartialOrd, Hash, derive_more::From)]
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
22 pub struct PyHgPathRef<'a>(pub &'a HgPath);
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
23
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
24 impl<'py> IntoPyObject<'py> for PyHgPathRef<'_> {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
25 type Target = PyBytes;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
26 type Output = Bound<'py, Self::Target>;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
27 type Error = Infallible;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
28
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
29 fn into_pyobject(
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
30 self,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
31 py: Python<'py>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
32 ) -> Result<Self::Output, Self::Error> {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
33 Ok(PyBytes::new(py, self.0.as_bytes()))
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
34 }
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
35 }
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
36
52862
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
37 #[derive(Eq, Ord, PartialEq, PartialOrd, Hash, derive_more::From)]
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
38 pub struct PyHgPathBuf(pub HgPathBuf);
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
39
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
40 // This is for now equivalent to taking a ref as `HgPath` and using
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
41 // `HgPathRef`. One day, perhaps, this variant for owned data could be
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
42 // implemented without allocation.
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
43 impl<'py> IntoPyObject<'py> for PyHgPathBuf {
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
44 type Target = PyBytes;
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
45 type Output = Bound<'py, Self::Target>;
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
46 type Error = Infallible;
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
47
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
48 fn into_pyobject(
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
49 self,
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
50 py: Python<'py>,
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
51 ) -> Result<Self::Output, Self::Error> {
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
52 Ok(PyBytes::new(py, self.0.as_bytes()))
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
53 }
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
54 }
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
55
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
56 pub struct PyHgPathDirstateV2Result<'a>(
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
57 pub Result<&'a HgPath, DirstateV2ParseError>,
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
58 );
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
59
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
60 impl<'py> IntoPyObject<'py> for PyHgPathDirstateV2Result<'_> {
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
61 type Target = PyBytes;
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
62 type Output = Bound<'py, Self::Target>;
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
63 type Error = PyErr;
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
64
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
65 fn into_pyobject(
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
66 self,
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
67 py: Python<'py>,
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
68 ) -> Result<Self::Output, Self::Error> {
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
69 Ok(PyBytes::new(
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
70 py,
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
71 self.0.map_err(dirstate_v2_error)?.as_bytes(),
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
72 ))
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
73 }
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
74 }
09eb477eec65 rust-pyo3-path: more conversions to Python
Georges Racinet <georges.racinet@cloudcrane.io>
parents: 52859
diff changeset
75
52858
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
76 #[allow(dead_code)]
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
77 pub fn paths_py_list<I, U>(
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
78 py: Python<'_>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
79 paths: impl IntoIterator<Item = I, IntoIter = U>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
80 ) -> PyResult<Py<PyList>>
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
81 where
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
82 I: AsRef<HgPath>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
83 U: ExactSizeIterator<Item = I>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
84 {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
85 Ok(PyList::new(
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
86 py,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
87 paths
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
88 .into_iter()
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
89 .map(|p| PyBytes::new(py, p.as_ref().as_bytes())),
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
90 )?
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
91 .unbind())
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
92 }
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
93
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
94 #[allow(dead_code)]
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
95 pub fn paths_pyiter_collect<C>(paths: &Bound<'_, PyAny>) -> PyResult<C>
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
96 where
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
97 C: FromIterator<HgPathBuf>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
98 {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
99 paths
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
100 .try_iter()?
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
101 .map(|p| {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
102 let path = p?;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
103 Ok(HgPathBuf::from_bytes(
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
104 path.downcast::<PyBytes>()?.as_bytes(),
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
105 ))
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
106 })
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
107 .collect()
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
108 }