Debian Codegraph @

rust/hg-pyo3/src/dirstate/copy_map.rs

raw
1 // copy_map.rs
2 //
3 // Copyright 2019 Raphaël Gomès <[email protected]>
4 // 2025 Georges Racinet <[email protected]>
5 //
6 // This software may be used and distributed according to the terms of the
7 // GNU General Public License version 2 or any later version.
8 //! Bindings for `hg::dirstate::dirstate_map::CopyMap` provided by the
9 //! `hg-core` package.
10
11 use std::sync::RwLockReadGuard;
12 use std::sync::RwLockWriteGuard;
13
14 use hg::dirstate::on_disk::DirstateV2ParseError;
15 use hg::dirstate::owning::OwningDirstateMap;
16 use hg::dirstate::CopyMapIter;
17 use hg::utils::hg_path::HgPath;
18 use pyo3::exceptions::PyKeyError;
19 use pyo3::prelude::*;
20 use pyo3::types::PyBytes;
21 use pyo3::types::PyDict;
22 use pyo3::types::PyTuple;
23 use pyo3_sharedref::py_shared_iterator;
24
25 use super::dirstate_map::DirstateMap;
26 use crate::exceptions::dirstate_v2_error;
27 use crate::path::PyHgPathBuf;
28 use crate::path::PyHgPathRef;
29
30 #[pyclass(mapping)]
31 pub struct CopyMap {
32 dirstate_map: Py<DirstateMap>,
33 }
34
35 #[pymethods]
36 impl CopyMap {
37 #[new]
38 pub fn new(dsm: &Bound<'_, DirstateMap>) -> PyResult<Self> {
39 Ok(Self { dirstate_map: dsm.clone().unbind() })
40 }
41
42 fn __getitem__(
43 &self,
44 py: Python,
45 key: &Bound<'_, PyBytes>,
46 ) -> PyResult<Py<PyBytes>> {
47 let key = key.as_bytes();
48 self.with_dirstate_map_read(py, |inner_dsm| {
49 inner_dsm
50 .copy_map_get(HgPath::new(key))
51 .map_err(dirstate_v2_error)?
52 .ok_or_else(|| {
53 PyKeyError::new_err(
54 String::from_utf8_lossy(key).to_string(),
55 )
56 })
57 .and_then(|copy| {
58 Ok(PyHgPathRef(copy).into_pyobject(py)?.unbind())
59 })
60 })
61 }
62
63 fn __len__(&self, py: Python) -> PyResult<usize> {
64 self.with_dirstate_map_read(
65 py,
66 |inner_dsm| Ok(inner_dsm.copy_map_len()),
67 )
68 }
69
70 fn __contains__(
71 &self,
72 py: Python,
73 key: &Bound<'_, PyBytes>,
74 ) -> PyResult<bool> {
75 let key = key.as_bytes();
76 self.with_dirstate_map_read(py, |inner_dsm| {
77 inner_dsm
78 .copy_map_contains_key(HgPath::new(key))
79 .map_err(dirstate_v2_error)
80 })
81 }
82
83 #[pyo3(signature = (key, default=None))]
84 fn get(
85 &self,
86 py: Python,
87 key: &Bound<'_, PyBytes>,
88 default: Option<Py<PyAny>>,
89 ) -> PyResult<Option<Py<PyAny>>> {
90 let key = key.as_bytes();
91 self.with_dirstate_map_read(py, |inner_dsm| {
92 match inner_dsm
93 .copy_map_get(HgPath::new(key))
94 .map_err(dirstate_v2_error)?
95 {
96 Some(copy) => Ok(Some(
97 PyHgPathRef(copy).into_pyobject(py)?.unbind().into(),
98 )),
99 None => Ok(default),
100 }
101 })
102 }
103
104 #[pyo3(signature = (key, default=None))]
105 fn pop(
106 &self,
107 py: Python,
108 key: &Bound<'_, PyBytes>,
109 default: Option<Py<PyAny>>,
110 ) -> PyResult<Option<Py<PyAny>>> {
111 let path = HgPath::new(key.as_bytes());
112 self.with_dirstate_map_write(py, |mut inner_dsm| {
113 match inner_dsm.copy_map_remove(path).map_err(dirstate_v2_error)? {
114 Some(copy) => Ok(Some(
115 PyHgPathBuf(copy).into_pyobject(py)?.unbind().into(),
116 )),
117 None => Ok(default),
118 }
119 })
120 }
121
122 fn __iter__(&self, py: Python) -> PyResult<CopyMapKeysIterator> {
123 self.keys(py)
124 }
125
126 fn keys(&self, py: Python) -> PyResult<CopyMapKeysIterator> {
127 CopyMapKeysIterator::new(self.dirstate_map.bind(py))
128 }
129
130 fn items(&self, py: Python) -> PyResult<CopyMapItemsIterator> {
131 CopyMapItemsIterator::new(self.dirstate_map.bind(py))
132 }
133
134 fn __setitem__(
135 &self,
136 py: Python,
137 key: &Bound<'_, PyBytes>,
138 value: &Bound<'_, PyBytes>,
139 ) -> PyResult<()> {
140 let key = HgPath::new(key.as_bytes());
141 let value = HgPath::new(value.as_bytes());
142 self.with_dirstate_map_write(py, |mut inner_dsm| {
143 inner_dsm.copy_map_insert(key, value).map_err(dirstate_v2_error)
144 })?;
145 Ok(())
146 }
147
148 fn copy(&self, py: Python) -> PyResult<Py<PyDict>> {
149 let dict = PyDict::new(py);
150 // The `IntoPyDict` trait just does the same, but is not applicable
151 // here because it is meant to work on infallible iterators
152 self.with_dirstate_map_read(py, |inner_dsm| {
153 for item in inner_dsm.copy_map_iter() {
154 let (key, value) = item.map_err(dirstate_v2_error)?;
155 dict.set_item(PyHgPathRef(key), PyHgPathRef(value))?;
156 }
157 Ok(())
158 })?;
159 Ok(dict.unbind())
160 }
161 }
162
163 py_shared_iterator!(
164 CopyMapKeysIterator,
165 PyBytes,
166 DirstateMap,
167 inner,
168 CopyMapIter<'static>,
169 |dsm| dsm.copy_map_iter(),
170 CopyMap::keys_next_result
171 );
172
173 py_shared_iterator!(
174 CopyMapItemsIterator,
175 PyTuple,
176 DirstateMap,
177 inner,
178 CopyMapIter<'static>,
179 |dsm| dsm.copy_map_iter(),
180 CopyMap::items_next_result
181 );
182
183 impl CopyMap {
184 fn keys_next_result(
185 py: Python,
186 res: Result<(&HgPath, &HgPath), DirstateV2ParseError>,
187 ) -> PyResult<Option<Py<PyBytes>>> {
188 let key = res.map_err(dirstate_v2_error)?.0;
189 Ok(Some(PyHgPathRef(key).into_pyobject(py)?.unbind()))
190 }
191
192 fn items_next_result(
193 py: Python,
194 res: Result<(&HgPath, &HgPath), DirstateV2ParseError>,
195 ) -> PyResult<Option<Py<PyTuple>>> {
196 let (key, value) = res.map_err(dirstate_v2_error)?;
197 Ok(Some(
198 (PyHgPathRef(key), PyHgPathRef(value)).into_pyobject(py)?.unbind(),
199 ))
200 }
201
202 fn with_dirstate_map_read<T>(
203 &self,
204 py: Python,
205 f: impl FnOnce(RwLockReadGuard<OwningDirstateMap>) -> PyResult<T>,
206 ) -> PyResult<T> {
207 let dsm = self.dirstate_map.bind(py);
208 DirstateMap::with_inner_read(dsm, |_dsm, inner| f(inner))
209 }
210
211 fn with_dirstate_map_write<T>(
212 &self,
213 py: Python,
214 f: impl FnOnce(RwLockWriteGuard<OwningDirstateMap>) -> PyResult<T>,
215 ) -> PyResult<T> {
216 let dsm = self.dirstate_map.bind(py);
217 DirstateMap::with_inner_write(dsm, |_dsm, inner| f(inner))
218 }
219 }