Mercurial > public > mercurial-scm > hg
annotate rust/hg-pyo3/src/path.rs @ 52858:c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Same as `PyRevision`, the `PyHgPathRef` new-type wrapper is not
a Python type, but it implements `IntoPyObject`, which will spare
us tedious uses of `PyBytes::new`.
The `paths_py_list` function is the the analog of `revs_py_list`
for paths. There was one similar utility in `hg-cpython`, within
the `dirstate::status` module. We feel it should be in a more global
location, and have a name consistent with utilities for revisions.
The `paths_pyiter_collect` function is similarly the analog of
`revs_pyiter_collect`.
author | Georges Racinet <georges.racinet@cloudcrane.io> |
---|---|
date | Thu, 06 Feb 2025 11:18:28 +0100 |
parents | |
children | 9f083ff3c96c |
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 |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
16 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
|
17 |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
18 #[allow(dead_code)] |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
19 #[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
|
20 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
|
21 |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
22 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
|
23 type Target = PyBytes; |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
24 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
|
25 type Error = Infallible; |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
26 |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
27 fn into_pyobject( |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
28 self, |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
29 py: Python<'py>, |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
30 ) -> 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
|
31 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
|
32 } |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
33 } |
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 #[allow(dead_code)] |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
36 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
|
37 py: Python<'_>, |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
38 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
|
39 ) -> PyResult<Py<PyList>> |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
40 where |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
41 I: AsRef<HgPath>, |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
42 U: ExactSizeIterator<Item = I>, |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
43 { |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
44 Ok(PyList::new( |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
45 py, |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
46 paths |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
47 .into_iter() |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
48 .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
|
49 )? |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
50 .unbind()) |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
51 } |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
52 |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
53 #[allow(dead_code)] |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
54 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
|
55 where |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
56 C: FromIterator<HgPathBuf>, |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
57 { |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
58 paths |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
59 .try_iter()? |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
60 .map(|p| { |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
61 let path = p?; |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
62 Ok(HgPathBuf::from_bytes( |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
63 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
|
64 )) |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
65 }) |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
66 .collect() |
c60f69556924
rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff
changeset
|
67 } |