Skip to main content

numra_interp/
error.rs

1//!
2//! Author: Moussa Leblouba
3//! Date: 9 February 2026
4//! Modified: 2 May 2026
5
6use core::fmt;
7use numra_core::NumraError;
8
9/// Errors that can occur during interpolation.
10#[derive(Clone, Debug, PartialEq)]
11pub enum InterpError {
12    /// Too few data points for the requested method.
13    TooFewPoints { got: usize, min: usize },
14    /// Data x-coordinates are not strictly increasing.
15    UnsortedData,
16    /// x and y arrays have different lengths.
17    DimensionMismatch { x_len: usize, y_len: usize },
18    /// Bad input.
19    InvalidInput(String),
20}
21
22impl fmt::Display for InterpError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            InterpError::TooFewPoints { got, min } => {
26                write!(f, "Too few data points: got {}, need at least {}", got, min)
27            }
28            InterpError::UnsortedData => {
29                write!(f, "Data x-coordinates must be strictly increasing")
30            }
31            InterpError::DimensionMismatch { x_len, y_len } => {
32                write!(
33                    f,
34                    "Dimension mismatch: x has {} elements, y has {}",
35                    x_len, y_len
36                )
37            }
38            InterpError::InvalidInput(msg) => {
39                write!(f, "Invalid input: {}", msg)
40            }
41        }
42    }
43}
44
45impl From<InterpError> for NumraError {
46    fn from(e: InterpError) -> Self {
47        NumraError::Interp(e.to_string())
48    }
49}