Patchwork [2,of,3,STABLE] rust: error for WdirUnsupported with cpython conversion as exception

login
register
mail settings
Submitter Georges Racinet
Date Jan. 24, 2019, 4:23 a.m.
Message ID <a35cfd592a90ae325b45.1548303832@ishtar>
Download mbox | patch
Permalink /patch/37952/
State Accepted
Headers show

Comments

Georges Racinet - Jan. 24, 2019, 4:23 a.m.
# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1548247624 18000
#      Wed Jan 23 07:47:04 2019 -0500
# Branch stable
# Node ID a35cfd592a90ae325b452c56fe8bff86cac097dd
# Parent  56b74abf5ee6df48ec2debf1f004725cfcc93854
# EXP-Topic rust-wdirunsupported
rust: error for WdirUnsupported with cpython conversion as exception

This introduces WorkingDirectoryUnsupported as an enum variant
of GraphError in the core and converts it to the expected
`mercurial.error.WdirUnsupported`.
Yuya Nishihara - Jan. 25, 2019, 8:44 a.m.
On Wed, 23 Jan 2019 23:23:52 -0500, Georges Racinet wrote:
> # HG changeset patch
> # User Georges Racinet <georges.racinet@octobus.net>
> # Date 1548247624 18000
> #      Wed Jan 23 07:47:04 2019 -0500
> # Branch stable
> # Node ID a35cfd592a90ae325b452c56fe8bff86cac097dd
> # Parent  56b74abf5ee6df48ec2debf1f004725cfcc93854
> # EXP-Topic rust-wdirunsupported
> rust: error for WdirUnsupported with cpython conversion as exception

> @@ -22,6 +24,15 @@
>              hg::GraphError::ParentOutOfRange(r) => {
>                  GraphError::new(py, ("ParentOutOfRange", r))
>              }
> +            hg::GraphError::WorkingDirectoryUnsupported => {
> +                match py
> +                    .import("mercurial.error")
> +                    .and_then(|m| m.get(py, "WdirUnsupported"))
> +                {
> +                    Err(e) => e,
> +                    Ok(cls) => PyErr::from_instance(py, cls),
> +                }

Can be spelled as .map(|cls| ...).unwrap_or_else(|e| e) if you prefer
method chain.

Patch

diff -r 56b74abf5ee6 -r a35cfd592a90 rust/hg-core/src/lib.rs
--- a/rust/hg-core/src/lib.rs	Wed Jan 23 07:39:27 2019 -0500
+++ b/rust/hg-core/src/lib.rs	Wed Jan 23 07:47:04 2019 -0500
@@ -33,4 +33,5 @@ 
 #[derive(Clone, Debug, PartialEq)]
 pub enum GraphError {
     ParentOutOfRange(Revision),
+    WorkingDirectoryUnsupported,
 }
diff -r 56b74abf5ee6 -r a35cfd592a90 rust/hg-cpython/src/exceptions.rs
--- a/rust/hg-cpython/src/exceptions.rs	Wed Jan 23 07:39:27 2019 -0500
+++ b/rust/hg-cpython/src/exceptions.rs	Wed Jan 23 07:47:04 2019 -0500
@@ -8,6 +8,8 @@ 
 //! Bindings for Rust errors
 //!
 //! [`GraphError`] exposes `hg::GraphError` as a subclass of `ValueError`
+//! but some variants of `hg::GraphError` can be converted directly to other
+//! existing Python exceptions if appropriate.
 //!
 //! [`GraphError`]: struct.GraphError.html
 use cpython::exc::ValueError;
@@ -22,6 +24,15 @@ 
             hg::GraphError::ParentOutOfRange(r) => {
                 GraphError::new(py, ("ParentOutOfRange", r))
             }
+            hg::GraphError::WorkingDirectoryUnsupported => {
+                match py
+                    .import("mercurial.error")
+                    .and_then(|m| m.get(py, "WdirUnsupported"))
+                {
+                    Err(e) => e,
+                    Ok(cls) => PyErr::from_instance(py, cls),
+                }
+            }
         }
     }
 }