rust: error for WdirUnsupported with cpython conversion as exception stable
authorGeorges Racinet <georges.racinet@octobus.net>
Wed, 23 Jan 2019 07:47:04 -0500
branchstable
changeset 41349 ee943a920606
parent 41348 2f54f31c41aa
child 41350 ab0d762d89ef
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`.
rust/hg-core/src/lib.rs
rust/hg-cpython/src/exceptions.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,
 }
--- 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),
+                }
+            }
         }
     }
 }