1use crate::{io, linalg, manifold, optimizer};
7use thiserror::Error;
8
9pub type ApexResult<T> = Result<T, ApexError>;
11
12#[derive(Debug, Clone, Error)]
14pub enum ApexError {
15 #[error("Linear algebra error: {0}")]
17 LinearAlgebra(String),
18
19 #[error("IO error: {0}")]
21 Io(String),
22
23 #[error("Manifold error: {0}")]
25 Manifold(String),
26
27 #[error("Solver error: {0}")]
29 Solver(String),
30
31 #[error("Computation error: {0}")]
33 Computation(String),
34
35 #[error("Invalid input: {0}")]
37 InvalidInput(String),
38
39 #[error("Memory error: {0}")]
41 Memory(String),
42
43 #[error("Convergence error: {0}")]
45 Convergence(String),
46
47 #[error("Matrix operation error: {0}")]
49 MatrixOperation(String),
50
51 #[error("Thread synchronization error: {0}")]
53 ThreadError(String),
54}
55
56impl From<std::io::Error> for ApexError {
59 fn from(err: std::io::Error) -> Self {
60 ApexError::Io(err.to_string())
61 }
62}
63
64impl From<std::num::ParseFloatError> for ApexError {
65 fn from(err: std::num::ParseFloatError) -> Self {
66 ApexError::InvalidInput(format!("Failed to parse float: {err}"))
67 }
68}
69
70impl From<std::num::ParseIntError> for ApexError {
71 fn from(err: std::num::ParseIntError) -> Self {
72 ApexError::InvalidInput(format!("Failed to parse integer: {err}"))
73 }
74}
75
76impl From<linalg::LinAlgError> for ApexError {
79 fn from(err: linalg::LinAlgError) -> Self {
80 ApexError::LinearAlgebra(err.to_string())
81 }
82}
83
84impl From<optimizer::OptimizerError> for ApexError {
85 fn from(err: optimizer::OptimizerError) -> Self {
86 ApexError::Solver(err.to_string())
87 }
88}
89
90impl From<manifold::ManifoldError> for ApexError {
91 fn from(err: manifold::ManifoldError) -> Self {
92 ApexError::Manifold(err.to_string())
93 }
94}
95
96impl From<io::ApexSolverIoError> for ApexError {
97 fn from(err: io::ApexSolverIoError) -> Self {
98 ApexError::Io(err.to_string())
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn test_apex_error_display() {
108 let error = ApexError::LinearAlgebra("Matrix is singular".to_string());
109 assert_eq!(
110 error.to_string(),
111 "Linear algebra error: Matrix is singular"
112 );
113 }
114
115 #[test]
116 fn test_apex_error_from_io() {
117 let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
118 let apex_error = ApexError::from(io_error);
119
120 match apex_error {
121 ApexError::Io(msg) => assert!(msg.contains("File not found")),
122 _ => panic!("Expected IO error"),
123 }
124 }
125
126 #[test]
127 fn test_apex_result_ok() {
128 let result: ApexResult<i32> = Ok(42);
129 assert!(result.is_ok());
130 if let Ok(value) = result {
131 assert_eq!(value, 42);
132 }
133 }
134
135 #[test]
136 fn test_apex_result_err() {
137 let result: ApexResult<i32> = Err(ApexError::Computation("Test error".to_string()));
138 assert!(result.is_err());
139 }
140}