rust-pyo3-exceptions: helper for ValueError by calling to_string
authorGeorges Racinet <georges.racinet@cloudcrane.io>
Wed, 05 Feb 2025 11:04:19 +0100
changeset 52863 ab6198160960
parent 52862 09eb477eec65
child 52864 d961e09d3d8c
rust-pyo3-exceptions: helper for ValueError by calling to_string This may seem trivial, but it allows to use `map_err(to_string_value_error)` in many places, instead of defining a closure `map_err(|e| PyValueError::new_err(e.to_string())`, and it is much better for readability.
rust/hg-pyo3/src/exceptions.rs
--- a/rust/hg-pyo3/src/exceptions.rs	Thu Feb 06 13:57:51 2025 +0100
+++ b/rust/hg-pyo3/src/exceptions.rs	Wed Feb 05 11:04:19 2025 +0100
@@ -2,6 +2,8 @@
 use pyo3::import_exception;
 use pyo3::{create_exception, PyErr};
 
+use std::fmt::Display;
+
 use hg::dirstate::{on_disk::DirstateV2ParseError, DirstateError};
 
 use hg::revlog::nodemap::NodeMapError;
@@ -50,6 +52,11 @@
     PyRuntimeError::new_err(format!("In Rust PyO3 bindings: {e}"))
 }
 
+#[allow(unused)]
+pub fn to_string_value_error<T: Display>(e: T) -> PyErr {
+    PyValueError::new_err(e.to_string())
+}
+
 pub mod mercurial_py_errors {
     pyo3::import_exception!(mercurial.error, RevlogError);
 }