suan_core/
common.rs

1// implement a common error type, and implement conversion to python types if enabled pyo3 feature
2
3/// A enum representation all possible errors for operations on `Subject`.
4pub enum SuErr {
5    /// Variant is unsupported for specific operation
6    UnsupportedVariant,
7
8    /// The input is not in the valid domain of the function
9    InvalidDomain,
10
11    /// The desired precision exceeds the maximum precision to be supported
12    ExceedMaxPrecision,
13}
14
15pub type SuResult<T> = Result<T, SuErr>;
16pub type SubjectResult = Result<crate::subject::Subject, SuErr>;
17
18#[cfg(feature = "pyo3")]
19mod pysupport {
20    use super::*;
21    use pyo3::exceptions::{PyOverflowError, PyTypeError, PyValueError};
22    use pyo3::PyErr;
23
24    impl From<SuErr> for PyErr {
25        fn from(e: SuErr) -> Self {
26            match e {
27                SuErr::UnsupportedVariant => {
28                    PyTypeError::new_err("this operation is not supported on this variant")
29                },
30                SuErr::InvalidDomain => {
31                    PyValueError::new_err("the input is not in the valid domain")
32                }
33                SuErr::ExceedMaxPrecision => {
34                    PyOverflowError::new_err("the precision exceeded limit during the operation")
35                }
36            }
37        }
38    }
39}