diff rust/hg-pyo3/src/exceptions.rs @ 52863:ab6198160960

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.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Wed, 05 Feb 2025 11:04:19 +0100
parents 9f083ff3c96c
children d961e09d3d8c
line wrap: on
line diff
--- 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);
 }