Skip to main content

vision_calibration_optim/
error.rs

1//! Typed error enum for `vision-calibration-optim`.
2
3use vision_calibration_core::Error as CoreError;
4
5/// Errors returned by public APIs in `vision-calibration-optim`.
6#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9    /// Input data is invalid (e.g. mismatched lengths, wrong parameter count).
10    #[error("invalid input: {reason}")]
11    InvalidInput { reason: String },
12
13    /// Not enough data to proceed.
14    #[error("insufficient data: need {need}, got {got}")]
15    InsufficientData { need: usize, got: usize },
16
17    /// A matrix inversion or decomposition produced a degenerate result.
18    #[error("singular matrix or degenerate configuration")]
19    Singular,
20
21    /// A numerical operation failed unexpectedly.
22    #[error("numerical failure: {0}")]
23    Numerical(String),
24
25    /// Forwarded error from `vision-calibration-core`.
26    #[error(transparent)]
27    Core(#[from] CoreError),
28}
29
30impl Error {
31    /// Convenience constructor for [`Error::InvalidInput`].
32    pub(crate) fn invalid_input(reason: impl Into<String>) -> Self {
33        Self::InvalidInput {
34            reason: reason.into(),
35        }
36    }
37
38    /// Convenience constructor for [`Error::Numerical`].
39    pub(crate) fn numerical(msg: impl Into<String>) -> Self {
40        Self::Numerical(msg.into())
41    }
42}
43
44impl From<anyhow::Error> for Error {
45    fn from(e: anyhow::Error) -> Self {
46        Self::Numerical(e.to_string())
47    }
48}