annotate rust/hg-cpython/src/filepatterns.rs @ 42609:326fdce22fb2

rust: switch hg-core and hg-cpython to rust 2018 edition Many interesting changes have happened in Rust since the Oxidation Plan was introduced, like the 2018 edition and procedural macros: - Opting in to the 2018 edition is a clear benefit in terms of future proofing, new (nice to have) syntactical sugar notwithstanding. It also has a new non-lexical, non-AST based borrow checker that has fewer bugs(!) and allows us to write correct code that in some cases would have been rejected by the old one. - Procedural macros allow us to use the PyO3 crate which maintainers have expressed the clear goal of compiling on stable, which would help in code maintainability compared to rust-cpython. In this patch are the following changes: - Removing most `extern crate` uses - Updating `use` clauses (`crate` keyword, nested `use`) - Removing `mod.rs` in favor of an aptly named module file Like discussed in the mailing list ( https://www.mercurial-scm.org/pipermail/mercurial-devel/2019-July/132316.html ), until Rust integration in Mercurial is considered to be out of the experimental phase, the maximum version of Rust allowed is whatever the latest version Debian packages. Differential Revision: https://phab.mercurial-scm.org/D6597
author Rapha?l Gom?s <rgomes@octobus.net>
date Tue, 02 Jul 2019 17:15:03 +0200
parents 9609430d3625
children 0247601869ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
1 // filepatterns.rs
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
2 //
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
3 // Copyright 2019, Georges Racinet <gracinet@anybox.fr>,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
4 // Raphaël Gomès <rgomes@octobus.net>
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
5 //
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
6 // This software may be used and distributed according to the terms of the
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
7 // GNU General Public License version 2 or any later version.
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
8
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
9 //! Bindings for the `hg::filepatterns` module provided by the
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
10 //! `hg-core` crate. From Python, this will be seen as `rustext.filepatterns`
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
11 //! and can be used as replacement for the the pure `filepatterns` Python module.
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
12 //!
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
13 use crate::exceptions::{PatternError, PatternFileError};
41056
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40978
diff changeset
14 use cpython::{
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
15 PyBytes, PyDict, PyModule, PyObject, PyResult, PyTuple, Python, ToPyObject,
41056
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40978
diff changeset
16 };
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
17 use hg::{build_single_regex, read_pattern_file, LineNumber, PatternTuple};
41056
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40978
diff changeset
18
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
19 /// Rust does not like functions with different return signatures.
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
20 /// The 3-tuple version is always returned by the hg-core function,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
21 /// the (potential) conversion is handled at this level since it is not likely
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
22 /// to have any measurable impact on performance.
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
23 ///
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
24 /// The Python implementation passes a function reference for `warn` instead
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
25 /// of a boolean that is used to emit warnings while parsing. The Rust
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
26 /// implementation chooses to accumulate the warnings and propagate them to
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
27 /// Python upon completion. See the `readpatternfile` function in `match.py`
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
28 /// for more details.
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
29 fn read_pattern_file_wrapper(
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
30 py: Python,
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
31 file_path: PyObject,
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
32 warn: bool,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
33 source_info: bool,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
34 ) -> PyResult<PyTuple> {
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
35 match read_pattern_file(file_path.extract::<PyBytes>(py)?.data(py), warn) {
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
36 Ok((patterns, warnings)) => {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
37 if source_info {
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
38 let itemgetter = |x: &PatternTuple| {
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
39 (PyBytes::new(py, &x.0), x.1, PyBytes::new(py, &x.2))
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
40 };
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
41 let results: Vec<(PyBytes, LineNumber, PyBytes)> =
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
42 patterns.iter().map(itemgetter).collect();
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
43 return Ok((results, warnings).to_py_object(py));
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
44 }
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
45 let itemgetter = |x: &PatternTuple| PyBytes::new(py, &x.0);
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
46 let results: Vec<PyBytes> =
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
47 patterns.iter().map(itemgetter).collect();
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
48 Ok((results, warnings).to_py_object(py))
41056
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40978
diff changeset
49 }
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
50 Err(e) => Err(PatternFileError::pynew(py, e)),
41056
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40978
diff changeset
51 }
d9f439fcdb4c rust-cpython: binding for AncestorsIterator
Georges Racinet <gracinet@anybox.fr>
parents: 40978
diff changeset
52 }
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
53
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
54 fn build_single_regex_wrapper(
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
55 py: Python,
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
56 kind: PyObject,
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
57 pat: PyObject,
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
58 globsuffix: PyObject,
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
59 ) -> PyResult<PyBytes> {
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
60 match build_single_regex(
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
61 kind.extract::<PyBytes>(py)?.data(py),
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
62 pat.extract::<PyBytes>(py)?.data(py),
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
63 globsuffix.extract::<PyBytes>(py)?.data(py),
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
64 ) {
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
65 Ok(regex) => Ok(PyBytes::new(py, &regex)),
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
66 Err(e) => Err(PatternError::pynew(py, e)),
41117
b31a41f24864 rust-cpython: binding for LazyAncestors
Georges Racinet <gracinet@anybox.fr>
parents: 41056
diff changeset
67 }
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
68 }
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
69
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
70 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
71 let dotted_name = &format!("{}.filepatterns", package);
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
72 let m = PyModule::new(py, dotted_name)?;
41188
006c9ce486fa rust-cpython: bindings for MissingAncestors
Georges Racinet <georges.racinet@octobus.net>
parents: 41187
diff changeset
73
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
74 m.add(py, "__package__", package)?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
75 m.add(
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
76 py,
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
77 "__doc__",
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
78 "Patterns files parsing - Rust implementation",
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
79 )?;
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
80 m.add(
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
81 py,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
82 "build_single_regex",
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
83 py_fn!(
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
84 py,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
85 build_single_regex_wrapper(
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
86 kind: PyObject,
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
87 pat: PyObject,
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
88 globsuffix: PyObject
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
89 )
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
90 ),
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
91 )?;
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
92 m.add(
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
93 py,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
94 "read_pattern_file",
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
95 py_fn!(
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
96 py,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
97 read_pattern_file_wrapper(
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
98 file_path: PyObject,
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
99 warn: bool,
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
100 source_info: bool
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
101 )
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
102 ),
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
103 )?;
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
104 m.add(py, "PatternError", py.get_type::<PatternError>())?;
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
105 let sys = PyModule::import(py, "sys")?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
106 let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
107 sys_modules.set_item(py, dotted_name, &m)?;
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41704
diff changeset
108
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
109 Ok(m)
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
110 }