use crate::dtype::DType;
use rustpython_vm::{
PyResult, VirtualMachine, builtins::PyBaseException, PyRef,
};
#[inline]
pub fn unsupported_dtype(
vm: &VirtualMachine,
op: &str,
dt: DType,
) -> PyRef<PyBaseException> {
vm.new_type_error(format!(
"operation {op:?} not supported for dtype {}",
dt.name_owned()
))
}
#[inline]
pub fn empty_array<T: Clone>() -> ndarray::ArrayD<T> {
ndarray::ArrayD::from_shape_vec(ndarray::IxDyn(&[0]), Vec::<T>::new())
.unwrap_or_else(|_| {
ndarray::ArrayD::from_shape_simple_fn(ndarray::IxDyn(&[0]), || {
std::process::abort()
})
})
}
#[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}")))
}
}