annotate rust/hg-pyo3/src/path.rs @ 52859:9f083ff3c96c

rust-pyo3-dirstate: DirstateMap simple read-only methods This takes care of all read-only methods except: - copymap methods - methods returning iterators These two categories will be done in forthcoming changesets.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Wed, 29 Jan 2025 18:26:10 +0100
parents c60f69556924
children 09eb477eec65
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
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 #[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
19 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
20
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
21 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
22 type Target = PyBytes;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
23 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
24 type Error = Infallible;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
25
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
26 fn into_pyobject(
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
27 self,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
28 py: Python<'py>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
29 ) -> 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
30 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
31 }
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 #[allow(dead_code)]
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
35 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
36 py: Python<'_>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
37 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
38 ) -> PyResult<Py<PyList>>
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
39 where
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
40 I: AsRef<HgPath>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
41 U: ExactSizeIterator<Item = I>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
42 {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
43 Ok(PyList::new(
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
44 py,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
45 paths
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
46 .into_iter()
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
47 .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
48 )?
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
49 .unbind())
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
50 }
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 #[allow(dead_code)]
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
53 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
54 where
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
55 C: FromIterator<HgPathBuf>,
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
56 {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
57 paths
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
58 .try_iter()?
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
59 .map(|p| {
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
60 let path = p?;
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
61 Ok(HgPathBuf::from_bytes(
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
62 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
63 ))
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 .collect()
c60f69556924 rust-pyo3: new module for conversions of HgPath and friends
Georges Racinet <georges.racinet@cloudcrane.io>
parents:
diff changeset
66 }