use crate::NumRs2Error;
use pyo3::exceptions::{PyIndexError, PyRuntimeError, PyTypeError, PyValueError};
use pyo3::prelude::*;
impl From<NumRs2Error> for PyErr {
fn from(err: NumRs2Error) -> PyErr {
match err {
NumRs2Error::ShapeMismatch { .. } => PyValueError::new_err(format!("{}", err)),
NumRs2Error::DimensionMismatch(_) => PyValueError::new_err(format!("{}", err)),
NumRs2Error::IndexOutOfBounds { .. } => PyIndexError::new_err(format!("{}", err)),
NumRs2Error::ConversionError(_) => PyTypeError::new_err(format!("{}", err)),
NumRs2Error::InvalidOperation(msg) if msg.contains("singular") => {
PyValueError::new_err("Singular matrix")
}
NumRs2Error::InvalidOperation(msg) if msg.contains("converge") => {
PyRuntimeError::new_err("Algorithm did not converge")
}
NumRs2Error::IOError(_) => PyRuntimeError::new_err(format!("{}", err)),
_ => PyRuntimeError::new_err(format!("{}", err)),
}
}
}
pub fn value_error(msg: impl Into<String>) -> PyErr {
PyValueError::new_err(msg.into())
}
pub fn type_error(msg: impl Into<String>) -> PyErr {
PyTypeError::new_err(msg.into())
}
pub fn index_error(msg: impl Into<String>) -> PyErr {
PyIndexError::new_err(msg.into())
}