dirs_multiset.rs
130 lines
| 3.9 KiB
| application/rls-services+xml
|
RustLexer
|
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; | ||||
|
r42997 | use std::convert::TryInto; | ||
|
r42991 | |||
use cpython::{ | ||||
|
r42997 | exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult, | ||
Python, | ||||
|
r42991 | }; | ||
|
r43115 | use crate::dirstate::extract_dirstate; | ||
use crate::ref_sharing::{PySharedRefCell, PySharedState}; | ||||
|
r43157 | use hg::{ | ||
DirsMultiset, DirsMultisetIter, DirstateMapError, DirstateParseError, | ||||
EntryState, | ||||
}; | ||||
|
r42991 | |||
py_class!(pub class Dirs |py| { | ||||
|
r43115 | data inner: PySharedRefCell<DirsMultiset>; | ||
|
r42997 | data py_shared_state: PySharedState; | ||
|
r42991 | |||
// `map` is either a `dict` or a flat iterator (usually a `set`, sometimes | ||||
// a `list`) | ||||
def __new__( | ||||
_cls, | ||||
map: PyObject, | ||||
skip: Option<PyObject> = None | ||||
) -> PyResult<Self> { | ||||
|
r42994 | let mut skip_state: Option<EntryState> = None; | ||
|
r42991 | if let Some(skip) = skip { | ||
|
r42994 | skip_state = Some( | ||
skip.extract::<PyBytes>(py)?.data(py)[0] | ||||
.try_into() | ||||
.map_err(|e: DirstateParseError| { | ||||
PyErr::new::<exc::ValueError, _>(py, e.to_string()) | ||||
})?, | ||||
); | ||||
|
r42991 | } | ||
|
r42993 | let inner = if let Ok(map) = map.cast_as::<PyDict>(py) { | ||
let dirstate = extract_dirstate(py, &map)?; | ||||
|
r43070 | DirsMultiset::from_dirstate(&dirstate, skip_state) | ||
|
r42991 | } else { | ||
let map: Result<Vec<Vec<u8>>, PyErr> = map | ||||
.iter(py)? | ||||
.map(|o| Ok(o?.extract::<PyBytes>(py)?.data(py).to_owned())) | ||||
.collect(); | ||||
|
r43070 | DirsMultiset::from_manifest(&map?) | ||
|
r42993 | }; | ||
|
r42991 | |||
|
r42997 | Self::create_instance( | ||
py, | ||||
|
r43115 | PySharedRefCell::new(inner), | ||
|
r42997 | PySharedState::default() | ||
) | ||||
|
r42991 | } | ||
def addpath(&self, path: PyObject) -> PyResult<PyObject> { | ||||
|
r42997 | self.borrow_mut(py)?.add_path( | ||
|
r42991 | path.extract::<PyBytes>(py)?.data(py), | ||
); | ||||
Ok(py.None()) | ||||
} | ||||
def delpath(&self, path: PyObject) -> PyResult<PyObject> { | ||||
|
r42997 | self.borrow_mut(py)?.delete_path( | ||
|
r42991 | path.extract::<PyBytes>(py)?.data(py), | ||
) | ||||
.and(Ok(py.None())) | ||||
.or_else(|e| { | ||||
match e { | ||||
DirstateMapError::PathNotFound(_p) => { | ||||
Err(PyErr::new::<exc::ValueError, _>( | ||||
py, | ||||
"expected a value, found none".to_string(), | ||||
)) | ||||
} | ||||
DirstateMapError::EmptyPath => { | ||||
Ok(py.None()) | ||||
} | ||||
} | ||||
}) | ||||
} | ||||
|
r42997 | def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> { | ||
|
r43117 | let (leak_handle, leaked_ref) = unsafe { self.leak_immutable(py)? }; | ||
|
r42997 | DirsMultisetKeysIterator::create_instance( | ||
py, | ||||
|
r43116 | RefCell::new(Some(leak_handle)), | ||
|
r43157 | RefCell::new(leaked_ref.iter()), | ||
|
r42997 | ) | ||
|
r42991 | } | ||
def __contains__(&self, item: PyObject) -> PyResult<bool> { | ||||
Ok(self | ||||
|
r42997 | .inner(py) | ||
|
r42991 | .borrow() | ||
|
r42995 | .contains(item.extract::<PyBytes>(py)?.data(py).as_ref())) | ||
|
r42991 | } | ||
}); | ||||
|
r42997 | |||
py_shared_ref!(Dirs, DirsMultiset, inner, DirsMultisetLeakedRef,); | ||||
impl Dirs { | ||||
pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> { | ||||
|
r43115 | Self::create_instance( | ||
py, | ||||
PySharedRefCell::new(d), | ||||
PySharedState::default(), | ||||
) | ||||
|
r42997 | } | ||
fn translate_key(py: Python, res: &Vec<u8>) -> PyResult<Option<PyBytes>> { | ||||
Ok(Some(PyBytes::new(py, res))) | ||||
} | ||||
} | ||||
|
r43159 | py_shared_iterator!( | ||
|
r42997 | DirsMultisetKeysIterator, | ||
DirsMultisetLeakedRef, | ||||
|
r43157 | DirsMultisetIter<'static>, | ||
|
r42997 | Dirs::translate_key, | ||
Option<PyBytes> | ||||
); | ||||