dirs_multiset.rs
109 lines
| 3.2 KiB
| application/rls-services+xml
|
RustLexer
Raphaël Gomès
|
r42991 | // dirs_multiset.rs | ||
// | ||||
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net> | ||||
// | ||||
// This software may be used and distributed according to the terms of the | ||||
// GNU General Public License version 2 or any later version. | ||||
//! Bindings for the `hg::dirstate::dirs_multiset` file provided by the | ||||
//! `hg-core` package. | ||||
use std::cell::RefCell; | ||||
use cpython::{ | ||||
Simon Sapin
|
r48833 | exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult, | ||
Python, UnsafePyLeaked, | ||||
Raphaël Gomès
|
r42991 | }; | ||
Yuya Nishihara
|
r43157 | use hg::{ | ||
Raphaël Gomès
|
r43227 | utils::hg_path::{HgPath, HgPathBuf}, | ||
Arseniy Alekseyev
|
r51428 | DirsMultiset, DirsMultisetIter, | ||
Yuya Nishihara
|
r43157 | }; | ||
Raphaël Gomès
|
r42991 | |||
py_class!(pub class Dirs |py| { | ||||
Yuya Nishihara
|
r44703 | @shared data inner: DirsMultiset; | ||
Raphaël Gomès
|
r42991 | |||
// `map` is either a `dict` or a flat iterator (usually a `set`, sometimes | ||||
// a `list`) | ||||
def __new__( | ||||
_cls, | ||||
map: PyObject, | ||||
) -> PyResult<Self> { | ||||
Simon Sapin
|
r48833 | let inner = if map.cast_as::<PyDict>(py).is_ok() { | ||
let err = "pathutil.dirs() with a dict should only be used by the Python dirstatemap \ | ||||
and should not be used when Rust is enabled"; | ||||
return Err(PyErr::new::<exc::TypeError, _>(py, err.to_string())) | ||||
Raphaël Gomès
|
r42991 | } else { | ||
Raphaël Gomès
|
r43227 | let map: Result<Vec<HgPathBuf>, PyErr> = map | ||
Raphaël Gomès
|
r42991 | .iter(py)? | ||
Raphaël Gomès
|
r43227 | .map(|o| { | ||
Ok(HgPathBuf::from_bytes( | ||||
o?.extract::<PyBytes>(py)?.data(py), | ||||
)) | ||||
}) | ||||
Raphaël Gomès
|
r42991 | .collect(); | ||
Yuya Nishihara
|
r43070 | DirsMultiset::from_manifest(&map?) | ||
Raphaël Gomès
|
r44315 | .map_err(|e| { | ||
PyErr::new::<exc::ValueError, _>(py, e.to_string()) | ||||
})? | ||||
Raphaël Gomès
|
r42993 | }; | ||
Raphaël Gomès
|
r42991 | |||
Yuya Nishihara
|
r44703 | Self::create_instance(py, inner) | ||
Raphaël Gomès
|
r42991 | } | ||
def addpath(&self, path: PyObject) -> PyResult<PyObject> { | ||||
Yuya Nishihara
|
r44702 | self.inner(py).borrow_mut().add_path( | ||
Raphaël Gomès
|
r43227 | HgPath::new(path.extract::<PyBytes>(py)?.data(py)), | ||
Arseniy Alekseyev
|
r51430 | ).and(Ok(py.None())).map_err(|e| PyErr::new::<exc::ValueError, _>( | ||
Raphaël Gomès
|
r44227 | py, | ||
e.to_string(), | ||||
Arseniy Alekseyev
|
r51430 | ) | ||
) | ||||
Raphaël Gomès
|
r42991 | } | ||
def delpath(&self, path: PyObject) -> PyResult<PyObject> { | ||||
Yuya Nishihara
|
r44702 | self.inner(py).borrow_mut().delete_path( | ||
Raphaël Gomès
|
r43227 | HgPath::new(path.extract::<PyBytes>(py)?.data(py)), | ||
Raphaël Gomès
|
r42991 | ) | ||
.and(Ok(py.None())) | ||||
Arseniy Alekseyev
|
r51430 | .map_err(|e| | ||
PyErr::new::<exc::ValueError, _>( | ||||
Raphaël Gomès
|
r42991 | py, | ||
Raphaël Gomès
|
r44227 | e.to_string(), | ||
Arseniy Alekseyev
|
r51430 | ) | ||
) | ||||
Raphaël Gomès
|
r42991 | } | ||
Raphaël Gomès
|
r42997 | def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> { | ||
Yuya Nishihara
|
r44702 | let leaked_ref = self.inner(py).leak_immutable(); | ||
Yuya Nishihara
|
r43161 | DirsMultisetKeysIterator::from_inner( | ||
Raphaël Gomès
|
r42997 | py, | ||
Yuya Nishihara
|
r43579 | unsafe { leaked_ref.map(py, |o| o.iter()) }, | ||
Raphaël Gomès
|
r42997 | ) | ||
Raphaël Gomès
|
r42991 | } | ||
def __contains__(&self, item: PyObject) -> PyResult<bool> { | ||||
Yuya Nishihara
|
r44702 | Ok(self.inner(py).borrow().contains(HgPath::new( | ||
Raphaël Gomès
|
r50809 | item.extract::<PyBytes>(py)?.data(py), | ||
Raphaël Gomès
|
r43227 | ))) | ||
Raphaël Gomès
|
r42991 | } | ||
}); | ||||
Raphaël Gomès
|
r42997 | |||
impl Dirs { | ||||
pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> { | ||||
Yuya Nishihara
|
r44703 | Self::create_instance(py, d) | ||
Raphaël Gomès
|
r42997 | } | ||
Raphaël Gomès
|
r43227 | fn translate_key( | ||
py: Python, | ||||
res: &HgPathBuf, | ||||
) -> PyResult<Option<PyBytes>> { | ||||
Raphaël Gomès
|
r45500 | Ok(Some(PyBytes::new(py, res.as_bytes()))) | ||
Raphaël Gomès
|
r42997 | } | ||
} | ||||
Yuya Nishihara
|
r43159 | py_shared_iterator!( | ||
Raphaël Gomès
|
r42997 | DirsMultisetKeysIterator, | ||
Yuya Nishihara
|
r44703 | UnsafePyLeaked<DirsMultisetIter<'static>>, | ||
Raphaël Gomès
|
r42997 | Dirs::translate_key, | ||
Option<PyBytes> | ||||
); | ||||