apex_solver/
error.rs

1//! Error types for the apex-solver library
2//!
3//! This module provides the main error and result types used throughout the library.
4//! All errors use the `thiserror` crate for automatic trait implementations.
5
6use crate::{
7    io::ApexSolverIoError, linalg::LinAlgError, manifold::ManifoldError, optimizer::OptimizerError,
8};
9use std::{
10    io::Error,
11    num::{ParseFloatError, ParseIntError},
12};
13use thiserror::Error;
14
15/// Main result type used throughout the apex-solver library
16pub type ApexResult<T> = Result<T, ApexError>;
17
18/// Main error type for the apex-solver library
19#[derive(Debug, Clone, Error)]
20pub enum ApexError {
21    /// Linear algebra related errors
22    #[error("Linear algebra error: {0}")]
23    LinearAlgebra(String),
24
25    /// IO related errors (file loading, parsing, etc.)
26    #[error("IO error: {0}")]
27    Io(String),
28
29    /// Manifold operations errors
30    #[error("Manifold error: {0}")]
31    Manifold(String),
32
33    /// Solver related errors
34    #[error("Solver error: {0}")]
35    Solver(String),
36
37    /// General computation errors
38    #[error("Computation error: {0}")]
39    Computation(String),
40
41    /// Invalid input parameters
42    #[error("Invalid input: {0}")]
43    InvalidInput(String),
44
45    /// Memory allocation or management errors
46    #[error("Memory error: {0}")]
47    Memory(String),
48
49    /// Convergence failures
50    #[error("Convergence error: {0}")]
51    Convergence(String),
52
53    /// Matrix operation errors
54    #[error("Matrix operation error: {0}")]
55    MatrixOperation(String),
56
57    /// Thread synchronization errors
58    #[error("Thread synchronization error: {0}")]
59    ThreadError(String),
60}
61
62// Conversions from standard library errors
63
64impl From<Error> for ApexError {
65    fn from(err: Error) -> Self {
66        ApexError::Io(err.to_string())
67    }
68}
69
70impl From<ParseFloatError> for ApexError {
71    fn from(err: ParseFloatError) -> Self {
72        ApexError::InvalidInput(format!("Failed to parse float: {err}"))
73    }
74}
75
76impl From<ParseIntError> for ApexError {
77    fn from(err: ParseIntError) -> Self {
78        ApexError::InvalidInput(format!("Failed to parse integer: {err}"))
79    }
80}
81
82// Convert module-specific errors to ApexError
83
84impl From<LinAlgError> for ApexError {
85    fn from(err: LinAlgError) -> Self {
86        ApexError::LinearAlgebra(err.to_string())
87    }
88}
89
90impl From<OptimizerError> for ApexError {
91    fn from(err: OptimizerError) -> Self {
92        ApexError::Solver(err.to_string())
93    }
94}
95
96impl From<ManifoldError> for ApexError {
97    fn from(err: ManifoldError) -> Self {
98        ApexError::Manifold(err.to_string())
99    }
100}
101
102impl From<ApexSolverIoError> for ApexError {
103    fn from(err: ApexSolverIoError) -> Self {
104        ApexError::Io(err.to_string())
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111    use std::io::ErrorKind;
112
113    #[test]
114    fn test_apex_error_display() {
115        let error = ApexError::LinearAlgebra("Matrix is singular".to_string());
116        assert_eq!(
117            error.to_string(),
118            "Linear algebra error: Matrix is singular"
119        );
120    }
121
122    #[test]
123    fn test_apex_error_from_io() {
124        let io_error = Error::new(ErrorKind::NotFound, "File not found");
125        let apex_error = ApexError::from(io_error);
126
127        match apex_error {
128            ApexError::Io(msg) => assert!(msg.contains("File not found")),
129            _ => panic!("Expected IO error"),
130        }
131    }
132
133    #[test]
134    fn test_apex_result_ok() {
135        let result: ApexResult<i32> = Ok(42);
136        assert!(result.is_ok());
137        if let Ok(value) = result {
138            assert_eq!(value, 42);
139        }
140    }
141
142    #[test]
143    fn test_apex_result_err() {
144        let result: ApexResult<i32> = Err(ApexError::Computation("Test error".to_string()));
145        assert!(result.is_err());
146    }
147}