Skip to main content

rlst_suitesparse/umfpack/
error.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4/// Errors returned by UMFPACK bindings.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum UmfpackError {
7    /// The input matrix has a zero-sized dimension.
8    EmptyMatrix,
9    /// UMFPACK's direct solver requires a square matrix.
10    NonSquareMatrix {
11        /// Number of rows in the matrix.
12        rows: usize,
13        /// Number of columns in the matrix.
14        columns: usize,
15    },
16    /// The CSR data does not satisfy the expected storage invariants.
17    InvalidCsr(&'static str),
18    /// A matrix dimension or sparse index cannot be represented by UMFPACK's
19    /// 64-bit index type.
20    IndexOutOfRange(usize),
21    /// A right-hand side or solution has the wrong length.
22    DimensionMismatch {
23        /// Required vector length.
24        expected: usize,
25        /// Actual vector length.
26        actual: usize,
27    },
28    /// An array argument is not contiguous in memory.
29    NonContiguousArray(&'static str),
30    /// An array argument does not expose its backing data.
31    DataUnavailable(&'static str),
32    /// UMFPACK returned a failure status.
33    NativeFailure {
34        /// Native operation that failed.
35        operation: &'static str,
36        /// UMFPACK status code.
37        status: i32,
38    },
39    /// UMFPACK reported success but did not return the expected native handle.
40    NullHandle(&'static str),
41}
42
43impl Display for UmfpackError {
44    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
45        match self {
46            Self::EmptyMatrix => write!(formatter, "UMFPACK does not accept an empty matrix"),
47            Self::NonSquareMatrix { rows, columns } => {
48                write!(
49                    formatter,
50                    "UMFPACK requires a square matrix, got {rows}x{columns}"
51                )
52            }
53            Self::InvalidCsr(message) => write!(formatter, "invalid CSR matrix: {message}"),
54            Self::IndexOutOfRange(index) => {
55                write!(
56                    formatter,
57                    "sparse index {index} cannot be represented by UMFPACK"
58                )
59            }
60            Self::DimensionMismatch { expected, actual } => {
61                write!(
62                    formatter,
63                    "dimension mismatch: expected {expected}, got {actual}"
64                )
65            }
66            Self::NonContiguousArray(name) => {
67                write!(formatter, "{name} must be stored contiguously")
68            }
69            Self::DataUnavailable(name) => write!(formatter, "{name} does not expose raw data"),
70            Self::NativeFailure { operation, status } => {
71                write!(formatter, "UMFPACK {operation} failed with status {status}")
72            }
73            Self::NullHandle(operation) => {
74                write!(formatter, "UMFPACK {operation} returned a null handle")
75            }
76        }
77    }
78}
79
80impl Error for UmfpackError {}
81
82/// Result type returned by this crate.
83pub type UmfpackResult<T> = Result<T, UmfpackError>;