Mercurial > hg
annotate rust/hg-cpython/src/dirstate/dirs_multiset.rs @ 42750:849e744b925d
rust-dirstate: improve API of `DirsMultiset`
- Use opaque `Iterator` type instead of implementation-specific one from
`HashMap`
- Make `DirsMultiset` behave like a set both in Rust and from Python
Differential Revision: https://phab.mercurial-scm.org/D6690
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Wed, 17 Jul 2019 11:37:43 +0200 |
parents | 7ceded4419a3 |
children | 30320c7bf79f |
rev | line source |
---|---|
42746
b3518b0baa47
rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
1 // dirs_multiset.rs |
42303
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
2 // |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net> |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
4 // |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
5 // This software may be used and distributed according to the terms of the |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
6 // GNU General Public License version 2 or any later version. |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
7 |
42746
b3518b0baa47
rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
8 //! Bindings for the `hg::dirstate::dirs_multiset` file provided by the |
42303
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
9 //! `hg-core` package. |
42746
b3518b0baa47
rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
10 |
b3518b0baa47
rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
11 use std::cell::RefCell; |
42303
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
12 |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
13 use cpython::{ |
42746
b3518b0baa47
rust-dirstate: create dirstate submodule in hg-cpython
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
14 exc, ObjectProtocol, PyBytes, PyDict, PyErr, PyObject, PyResult, |
42750
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
15 PythonObject, ToPyObject, |
42303
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
16 }; |
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
17 |
42748
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
18 use crate::dirstate::extract_dirstate; |
42749
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
19 use hg::{ |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
20 DirsIterable, DirsMultiset, DirstateMapError, DirstateParseError, |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
21 EntryState, |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
22 }; |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
23 use std::convert::TryInto; |
42303
e240bec26626
rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
24 |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
25 py_class!(pub class Dirs |py| { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
26 data dirs_map: RefCell<DirsMultiset>; |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
27 |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
28 // `map` is either a `dict` or a flat iterator (usually a `set`, sometimes |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
29 // a `list`) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
30 def __new__( |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
31 _cls, |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
32 map: PyObject, |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
33 skip: Option<PyObject> = None |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
34 ) -> PyResult<Self> { |
42749
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
35 let mut skip_state: Option<EntryState> = None; |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
36 if let Some(skip) = skip { |
42749
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
37 skip_state = Some( |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
38 skip.extract::<PyBytes>(py)?.data(py)[0] |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
39 .try_into() |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
40 .map_err(|e: DirstateParseError| { |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
41 PyErr::new::<exc::ValueError, _>(py, e.to_string()) |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
42 })?, |
7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents:
42748
diff
changeset
|
43 ); |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
44 } |
42748
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
45 let inner = if let Ok(map) = map.cast_as::<PyDict>(py) { |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
46 let dirstate = extract_dirstate(py, &map)?; |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
47 DirsMultiset::new( |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
48 DirsIterable::Dirstate(&dirstate), |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
49 skip_state, |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
50 ) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
51 } else { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
52 let map: Result<Vec<Vec<u8>>, PyErr> = map |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
53 .iter(py)? |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
54 .map(|o| Ok(o?.extract::<PyBytes>(py)?.data(py).to_owned())) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
55 .collect(); |
42748
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
56 DirsMultiset::new( |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
57 DirsIterable::Manifest(&map?), |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
58 skip_state, |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
59 ) |
42748
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
60 }; |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
61 |
42748
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42746
diff
changeset
|
62 Self::create_instance(py, RefCell::new(inner)) |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
63 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
64 |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
65 def addpath(&self, path: PyObject) -> PyResult<PyObject> { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
66 self.dirs_map(py).borrow_mut().add_path( |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
67 path.extract::<PyBytes>(py)?.data(py), |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
68 ); |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
69 Ok(py.None()) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
70 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
71 |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
72 def delpath(&self, path: PyObject) -> PyResult<PyObject> { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
73 self.dirs_map(py).borrow_mut().delete_path( |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
74 path.extract::<PyBytes>(py)?.data(py), |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
75 ) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
76 .and(Ok(py.None())) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
77 .or_else(|e| { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
78 match e { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
79 DirstateMapError::PathNotFound(_p) => { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
80 Err(PyErr::new::<exc::ValueError, _>( |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
81 py, |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
82 "expected a value, found none".to_string(), |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
83 )) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
84 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
85 DirstateMapError::EmptyPath => { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
86 Ok(py.None()) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
87 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
88 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
89 }) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
90 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
91 |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
92 // This is really inefficient on top of being ugly, but it's an easy way |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
93 // of having it work to continue working on the rest of the module |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
94 // hopefully bypassing Python entirely pretty soon. |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
95 def __iter__(&self) -> PyResult<PyObject> { |
42750
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
96 let dirs = self.dirs_map(py).borrow(); |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
97 let dirs: Vec<_> = dirs |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
98 .iter() |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
99 .map(|d| PyBytes::new(py, d)) |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
100 .collect(); |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
101 dirs.to_py_object(py) |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
102 .into_object() |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
103 .iter(py) |
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
104 .map(|o| o.into_object()) |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
105 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
106 |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
107 def __contains__(&self, item: PyObject) -> PyResult<bool> { |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
108 Ok(self |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
109 .dirs_map(py) |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
110 .borrow() |
42750
849e744b925d
rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents:
42749
diff
changeset
|
111 .contains(item.extract::<PyBytes>(py)?.data(py).as_ref())) |
42537
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
112 } |
ce94f9622acd
rust-dirstate: add "dirs" rust-cpython binding
Raphaël Gomès <rgomes@octobus.net>
parents:
42408
diff
changeset
|
113 }); |