Skip to main content

converge_optimization/
error.rs

1//! Error types for converge-optimization
2
3use thiserror::Error;
4
5/// Result type alias using our Error type
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during optimization
9#[derive(Error, Debug, Clone, PartialEq)]
10pub enum Error {
11    /// Problem has no feasible solution
12    #[error("infeasible: {0}")]
13    Infeasible(String),
14
15    /// Problem is unbounded (no finite optimal)
16    #[error("unbounded: {0}")]
17    Unbounded(String),
18
19    /// Invalid input data
20    #[error("invalid input: {0}")]
21    InvalidInput(String),
22
23    /// Dimension mismatch in input
24    #[error("dimension mismatch: expected {expected}, got {got}")]
25    DimensionMismatch {
26        /// Expected dimension
27        expected: usize,
28        /// Actual dimension
29        got: usize,
30    },
31
32    /// Solver timeout
33    #[error("timeout after {seconds} seconds")]
34    Timeout {
35        /// Seconds elapsed before timeout
36        seconds: f64,
37    },
38
39    /// Numeric overflow during computation
40    #[error("numeric overflow: {0}")]
41    Overflow(String),
42
43    /// Algorithm did not converge
44    #[error("did not converge after {iterations} iterations")]
45    NoConvergence {
46        /// Iterations completed
47        iterations: usize,
48    },
49
50    /// Feature requires FFI but it's not enabled
51    #[error("FFI feature required for {0}")]
52    FfiRequired(String),
53
54    /// Internal error (bug)
55    #[error("internal error: {0}")]
56    Internal(String),
57}
58
59impl Error {
60    /// Create an infeasible error
61    pub fn infeasible(msg: impl Into<String>) -> Self {
62        Self::Infeasible(msg.into())
63    }
64
65    /// Create an invalid input error
66    pub fn invalid_input(msg: impl Into<String>) -> Self {
67        Self::InvalidInput(msg.into())
68    }
69
70    /// Create a dimension mismatch error
71    pub fn dimension_mismatch(expected: usize, got: usize) -> Self {
72        Self::DimensionMismatch { expected, got }
73    }
74
75    /// Create a timeout error
76    pub fn timeout(seconds: f64) -> Self {
77        Self::Timeout { seconds }
78    }
79}