rust/hg-cpython/src/dirstate/non_normal_entries.rs
author Simon Sapin <simon.sapin@octobus.net>
Wed, 19 May 2021 13:15:00 +0200
changeset 47335 ed1583a845d2
parent 47124 cd8ca38fccff
child 47692 e5fb14a07866
permissions -rw-r--r--
dirstate-v2: Make more APIs fallible, returning Result When parsing becomes lazy, parse error will potentially happen in more places. This propagates such errors to callers. Differential Revision: https://phab.mercurial-scm.org/D10749
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// non_normal_other_parent_entries.rs
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2020 Raphaël Gomès <rgomes@octobus.net>
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
use cpython::{
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
     9
    exc::NotImplementedError, CompareOp, ObjectProtocol, PyBytes, PyClone,
47094
e061a1df32a8 dirstate-tree: Abstract "non-normal" and "other parent" sets
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
    10
    PyErr, PyObject, PyResult, PyString, Python, PythonObject, ToPyObject,
e061a1df32a8 dirstate-tree: Abstract "non-normal" and "other parent" sets
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
    11
    UnsafePyLeaked,
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
};
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    14
use crate::dirstate::dirstate_map::v2_error;
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    15
use crate::dirstate::DirstateMap;
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    16
use hg::dirstate_tree::on_disk::DirstateV2ParseError;
47124
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47094
diff changeset
    17
use hg::utils::hg_path::HgPath;
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    18
use std::cell::RefCell;
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
py_class!(pub class NonNormalEntries |py| {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
    data dmap: DirstateMap;
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
    def __contains__(&self, key: PyObject) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
        self.dmap(py).non_normal_entries_contains(py, key)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
    def remove(&self, key: PyObject) -> PyResult<PyObject> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
        self.dmap(py).non_normal_entries_remove(py, key)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
    def __richcmp__(&self, other: PyObject, op: CompareOp) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
        match op {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
            CompareOp::Eq => self.is_equal_to(py, other),
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
            CompareOp::Ne => Ok(!self.is_equal_to(py, other)?),
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
            _ => Err(PyErr::new::<NotImplementedError, _>(py, ""))
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
        }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
    def __repr__(&self) -> PyResult<PyString> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
        self.dmap(py).non_normal_entries_display(py)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
    }
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    39
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    40
    def __iter__(&self) -> PyResult<NonNormalEntriesIterator> {
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    41
        self.dmap(py).non_normal_entries_iter(py)
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    42
    }
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
});
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
impl NonNormalEntries {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
    pub fn from_inner(py: Python, dm: DirstateMap) -> PyResult<Self> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
        Self::create_instance(py, dm)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
    fn is_equal_to(&self, py: Python, other: PyObject) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
        for item in other.iter(py)? {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
            if !self.dmap(py).non_normal_entries_contains(py, item?)? {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
                return Ok(false);
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
            }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
        }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
        Ok(true)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
    }
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    58
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    59
    fn translate_key(
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    60
        py: Python,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    61
        key: Result<&HgPath, DirstateV2ParseError>,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    62
    ) -> PyResult<Option<PyBytes>> {
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    63
        let key = key.map_err(|e| v2_error(py, e))?;
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44416
diff changeset
    64
        Ok(Some(PyBytes::new(py, key.as_bytes())))
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    65
    }
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
}
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    67
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    68
type NonNormalEntriesIter<'a> = Box<
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    69
    dyn Iterator<Item = Result<&'a HgPath, DirstateV2ParseError>> + Send + 'a,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    70
>;
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    71
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    72
py_shared_iterator!(
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    73
    NonNormalEntriesIterator,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    74
    UnsafePyLeaked<NonNormalEntriesIter<'static>>,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    75
    NonNormalEntries::translate_key,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    76
    Option<PyBytes>
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    77
);