use rustpython_vm::{
PyResult, VirtualMachine, builtins::PyBaseException, PyRef,
};
#[inline]
pub fn internal(vm: &VirtualMachine, what: impl AsRef<str>) -> PyRef<PyBaseException> {
vm.new_runtime_error(format!("rumpy internal: {}", what.as_ref()))
}
pub trait OptionExt<T> {
fn or_internal(self, vm: &VirtualMachine, what: &str) -> PyResult<T>;
}
impl<T> OptionExt<T> for Option<T> {
#[inline]
fn or_internal(self, vm: &VirtualMachine, what: &str) -> PyResult<T> {
match self {
Some(v) => Ok(v),
None => Err(internal(vm, what)),
}
}
}
pub trait ResultExt<T, E> {
fn or_internal(self, vm: &VirtualMachine, what: &str) -> PyResult<T>;
}
impl<T, E: std::fmt::Display> ResultExt<T, E> for Result<T, E> {
#[inline]
fn or_internal(self, vm: &VirtualMachine, what: &str) -> PyResult<T> {
self.map_err(|e| internal(vm, format!("{what}: {e}")))
}
}