rust-pyo3: conversions to GraphError Python exception
The nice thing is that with PyO3, exceptions can be instantiated
without holding the GIL. Hence the only thing that prevents us to
implement `Into<PyErr>` for `hg::GraphError` is that neither is
defined by the current crate.
We could use a wrapping "newtype", but the compiler is not so clever yet that
it could chain automatically to two needed `into()`, so we'll end up with
some type conversion anyway, involving something like `GraphErrorWrapper`.
At this point, explicitly named methods are just simpler.
--- a/rust/Cargo.lock Sat Nov 30 20:57:02 2024 +0100
+++ b/rust/Cargo.lock Thu Dec 05 14:36:40 2024 +0100
@@ -722,6 +722,7 @@
"pyo3",
"python3-sys",
"stable_deref_trait",
+ "vcsgraph",
]
[[package]]
--- a/rust/hg-pyo3/Cargo.toml Sat Nov 30 20:57:02 2024 +0100
+++ b/rust/hg-pyo3/Cargo.toml Thu Dec 05 14:36:40 2024 +0100
@@ -18,4 +18,5 @@
derive_more = "0.99.17"
env_logger = "0.9.3"
lazy_static = "*"
+vcsgraph = "0.2.0"
--- a/rust/hg-pyo3/src/exceptions.rs Sat Nov 30 20:57:02 2024 +0100
+++ b/rust/hg-pyo3/src/exceptions.rs Thu Dec 05 14:36:40 2024 +0100
@@ -1,4 +1,34 @@
-use pyo3::create_exception;
use pyo3::exceptions::PyValueError;
+use pyo3::import_exception;
+use pyo3::{create_exception, PyErr};
+
+use crate::revision::PyRevision;
create_exception!(pyo3_rustext, GraphError, PyValueError);
+import_exception!(mercurial.error, WdirUnsupported);
+
+impl GraphError {
+ pub fn from_hg(inner: hg::GraphError) -> PyErr {
+ match inner {
+ hg::GraphError::ParentOutOfRange(r) => {
+ GraphError::new_err(("ParentOutOfRange", PyRevision(r.0)))
+ }
+ }
+ }
+ pub fn from_vcsgraph(inner: vcsgraph::graph::GraphReadError) -> PyErr {
+ match inner {
+ vcsgraph::graph::GraphReadError::InconsistentGraphData => {
+ GraphError::new_err("InconsistentGraphData")
+ }
+ vcsgraph::graph::GraphReadError::InvalidKey => {
+ GraphError::new_err("ParentOutOfRange")
+ }
+ vcsgraph::graph::GraphReadError::KeyedInvalidKey(r) => {
+ GraphError::new_err(("ParentOutOfRange", r))
+ }
+ vcsgraph::graph::GraphReadError::WorkingDirectoryUnsupported => {
+ WdirUnsupported::new_err(())
+ }
+ }
+ }
+}