Skip to main content

single_svdlib/
error.rs

1use thiserror::Error;
2
3/// Everything this crate can fail with.
4///
5/// In 1.x the Lanczos path returned `SvdLibError` while the randomized path returned
6/// `anyhow::Error`; both now return this type.
7#[derive(Error, Debug, Clone, PartialEq)]
8pub enum SvdLibError {
9    /// A caller-supplied parameter was rejected before any work started.
10    #[error("invalid argument: {0}")]
11    InvalidArgument(String),
12
13    /// The operands' shapes are inconsistent.
14    #[error("shape mismatch: {0}")]
15    ShapeMismatch(String),
16
17    /// A tridiagonal/bidiagonal eigenproblem failed to converge.
18    ///
19    /// `stage` names the kernel (`imtqlb`, `imtql2`, ...) so the origin stays visible
20    /// without a separate variant per call site.
21    #[error("{stage}: no convergence after {iterations} iterations")]
22    NoConvergence {
23        stage: &'static str,
24        iterations: usize,
25    },
26
27    /// The algorithm ran but could not produce the requested number of dimensions.
28    #[error("{stage}: {message}")]
29    Failed {
30        stage: &'static str,
31        message: String,
32    },
33
34    /// A dense factorization from the linear-algebra backend failed.
35    #[error("dense {factorization} failed: {message}")]
36    DenseFactorization {
37        factorization: &'static str,
38        message: String,
39    },
40
41    #[error("ndarray shape error: {0}")]
42    Shape(String),
43}
44
45impl From<ndarray::ShapeError> for SvdLibError {
46    fn from(e: ndarray::ShapeError) -> Self {
47        SvdLibError::Shape(e.to_string())
48    }
49}
50
51impl SvdLibError {
52    pub(crate) fn invalid(msg: impl Into<String>) -> Self {
53        SvdLibError::InvalidArgument(msg.into())
54    }
55
56    pub(crate) fn failed(stage: &'static str, msg: impl Into<String>) -> Self {
57        SvdLibError::Failed {
58            stage,
59            message: msg.into(),
60        }
61    }
62
63    pub(crate) fn shape(msg: impl Into<String>) -> Self {
64        SvdLibError::ShapeMismatch(msg.into())
65    }
66}
67
68/// Convenience alias used throughout the crate.
69pub type Result<T> = std::result::Result<T, SvdLibError>;