annotate rust/hg-cpython/src/exceptions.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 d26e4a434fe5
children 30320c7bf79f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41184
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
1 // ancestors.rs
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
2 //
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
3 // Copyright 2018 Georges Racinet <gracinet@anybox.fr>
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
4 //
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
5 // This software may be used and distributed according to the terms of the
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
6 // GNU General Public License version 2 or any later version.
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
7
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
8 //! Bindings for Rust errors
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
9 //!
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
10 //! [`GraphError`] exposes `hg::GraphError` as a subclass of `ValueError`
41309
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
11 //! but some variants of `hg::GraphError` can be converted directly to other
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
12 //! existing Python exceptions if appropriate.
41184
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
13 //!
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 40978
diff changeset
14 //! [`GraphError`]: struct.GraphError.html
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42560
diff changeset
15 use cpython::{
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42560
diff changeset
16 exc::{IOError, RuntimeError, ValueError},
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42560
diff changeset
17 py_exception, PyErr, Python,
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42560
diff changeset
18 };
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
19 use hg;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
20
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
21 py_exception!(rustext, GraphError, ValueError);
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
22
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
23 impl GraphError {
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
24 pub fn pynew(py: Python, inner: hg::GraphError) -> PyErr {
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
25 match inner {
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
26 hg::GraphError::ParentOutOfRange(r) => {
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
27 GraphError::new(py, ("ParentOutOfRange", r))
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
28 }
41309
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
29 hg::GraphError::WorkingDirectoryUnsupported => {
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
30 match py
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
31 .import("mercurial.error")
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
32 .and_then(|m| m.get(py, "WdirUnsupported"))
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
33 {
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
34 Err(e) => e,
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
35 Ok(cls) => PyErr::from_instance(py, cls),
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
36 }
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
37 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
38 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
39 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
40 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
41
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
42 py_exception!(rustext, PatternError, RuntimeError);
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
43 py_exception!(rustext, PatternFileError, RuntimeError);
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
44
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
45 impl PatternError {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
46 pub fn pynew(py: Python, inner: hg::PatternError) -> PyErr {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
47 match inner {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
48 hg::PatternError::UnsupportedSyntax(m) => {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
49 PatternError::new(py, ("PatternError", m))
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
50 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
51 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
52 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
53 }
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
54
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
55 impl PatternFileError {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
56 pub fn pynew(py: Python, inner: hg::PatternFileError) -> PyErr {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
57 match inner {
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
58 hg::PatternFileError::IO(e) => {
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
59 let value = (e.raw_os_error().unwrap_or(2), e.to_string());
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42560
diff changeset
60 PyErr::new::<IOError, _>(py, value)
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 41309
diff changeset
61 }
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
62 hg::PatternFileError::Pattern(e, l) => match e {
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
63 hg::PatternError::UnsupportedSyntax(m) => {
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
64 PatternFileError::new(py, ("PatternFileError", m, l))
41309
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
65 }
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42350
diff changeset
66 },
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
67 }
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
68 }
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
69 }