use std::fmt;
pub type Result<T> = std::result::Result<T, PyrucastError>;
#[derive(Debug)]
pub enum PyrucastError {
Io(String),
Serialization(String),
Interrupted,
MeshSealed,
Message(String),
}
impl fmt::Display for PyrucastError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PyrucastError::Io(m) => write!(f, "I/O error: {m}"),
PyrucastError::Serialization(m) => write!(f, "serialization error: {m}"),
PyrucastError::Interrupted => write!(f, "computation interrupted"),
PyrucastError::MeshSealed => write!(
f,
"submesh is sealed: it is already used by a finite-element space, \
field or matrix and can no longer be modified"
),
PyrucastError::Message(m) => write!(f, "{m}"),
}
}
}
impl std::error::Error for PyrucastError {}
impl From<std::io::Error> for PyrucastError {
fn from(e: std::io::Error) -> Self {
PyrucastError::Io(e.to_string())
}
}
#[cfg(feature = "python-api")]
impl From<PyrucastError> for pyo3::PyErr {
fn from(e: PyrucastError) -> Self {
match e {
PyrucastError::Interrupted => {
pyo3::exceptions::PyKeyboardInterrupt::new_err(e.to_string())
}
_ => pyo3::exceptions::PyRuntimeError::new_err(e.to_string()),
}
}
}