Skip to main content

roche/
errors.rs

1use pyo3::exceptions::{PyRuntimeError, PyValueError};
2use pyo3::prelude::*;
3use std::fmt;
4
5#[derive(Debug)]
6pub enum RocheError {
7    // error in Dbrent function
8    DbrentError(String),
9    // error in lin_min function
10    LinminError(String),
11    // error in pot_min function
12    PotminError(String),
13    // Parameter error
14    ParameterError(String),
15    // error in Face function
16    FaceError(String),
17    // error in rtsafe function
18    RtsafeError(String),
19    // error in wd_phases function
20    WdphasesError(String),
21}
22
23impl std::error::Error for RocheError {}
24
25impl fmt::Display for RocheError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            RocheError::DbrentError(msg) => write!(f, "{}", msg),
29            RocheError::LinminError(msg) => write!(f, "{}", msg),
30            RocheError::PotminError(msg) => write!(f, "{}", msg),
31            RocheError::ParameterError(msg) => write!(f, "{}", msg),
32            RocheError::FaceError(msg) => write!(f, "{}", msg),
33            RocheError::RtsafeError(msg) => write!(f, "{}", msg),
34            RocheError::WdphasesError(msg) => write!(f, "{}", msg),
35        }
36    }
37}
38
39impl std::convert::From<RocheError> for PyErr {
40    fn from(err: RocheError) -> PyErr {
41        match err {
42            RocheError::DbrentError(_) => PyRuntimeError::new_err(err.to_string()),
43            RocheError::LinminError(_) => PyRuntimeError::new_err(err.to_string()),
44            RocheError::PotminError(_) => PyRuntimeError::new_err(err.to_string()),
45            RocheError::ParameterError(_) => PyValueError::new_err(err.to_string()),
46            RocheError::FaceError(_) => PyRuntimeError::new_err(err.to_string()),
47            RocheError::RtsafeError(_) => PyRuntimeError::new_err(err.to_string()),
48            RocheError::WdphasesError(_) => PyRuntimeError::new_err(err.to_string()),
49        }
50    }
51}