Skip to main content

yield_curves/
error.rs

1use std::fmt;
2
3/// Errors returned by curve construction.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum YieldCurveError {
6    /// Not enough points for the requested method.
7    InsufficientData {
8        method: &'static str,
9        need: usize,
10        got: usize,
11    },
12    /// A point failed validation (NaN/infinite, negative x, duplicate x).
13    InvalidPoint(String),
14    /// Parametric fit (Nelson-Siegel, Svensson) did not converge or produced
15    /// implausible parameters.
16    FitFailed(String),
17    /// Forward-rate computation received `t1 >= t2`, non-finite times, or
18    /// produced a non-finite result (e.g. taking a negative discount factor
19    /// to a fractional power).
20    InvalidTimeRange(String),
21}
22
23impl fmt::Display for YieldCurveError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::InsufficientData { method, need, got } => {
27                write!(f, "{method} requires at least {need} points, got {got}")
28            }
29            Self::InvalidPoint(msg) => write!(f, "invalid point: {msg}"),
30            Self::FitFailed(msg) => write!(f, "fit failed to converge: {msg}"),
31            Self::InvalidTimeRange(msg) => write!(f, "invalid time range: {msg}"),
32        }
33    }
34}
35
36impl std::error::Error for YieldCurveError {}