use std::fmt;
pub type InterpolateResult<T> = Result<T, InterpolateError>;
#[derive(Debug, Clone)]
pub enum InterpolateError {
ShapeMismatch {
expected: usize,
actual: usize,
context: String,
},
InsufficientData {
required: usize,
actual: usize,
context: String,
},
OutOfDomain {
point: f64,
min: f64,
max: f64,
context: String,
},
OutOfDomainNd {
dimension: usize,
point: f64,
min: f64,
max: f64,
context: String,
},
DimensionMismatch {
expected: usize,
actual: usize,
context: String,
},
NotMonotonic { context: String },
NumericalError { message: String },
InvalidParameter { parameter: String, message: String },
NumrError(String),
}
impl fmt::Display for InterpolateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ShapeMismatch {
expected,
actual,
context,
} => {
write!(
f,
"Shape mismatch in {}: expected {}, got {}",
context, expected, actual
)
}
Self::InsufficientData {
required,
actual,
context,
} => {
write!(
f,
"Insufficient data for {}: need at least {}, got {}",
context, required, actual
)
}
Self::OutOfDomain {
point,
min,
max,
context,
} => {
write!(
f,
"Point {} is outside interpolation domain [{}, {}] in {}",
point, min, max, context
)
}
Self::OutOfDomainNd {
dimension,
point,
min,
max,
context,
} => {
write!(
f,
"Point {} in dimension {} is outside domain [{}, {}] in {}",
point, dimension, min, max, context
)
}
Self::DimensionMismatch {
expected,
actual,
context,
} => {
write!(
f,
"Dimension mismatch in {}: expected {}, got {}",
context, expected, actual
)
}
Self::NotMonotonic { context } => {
write!(
f,
"Input x values must be strictly increasing in {}",
context
)
}
Self::NumericalError { message } => {
write!(f, "Numerical error: {}", message)
}
Self::InvalidParameter { parameter, message } => {
write!(f, "Invalid parameter '{}': {}", parameter, message)
}
Self::NumrError(msg) => {
write!(f, "numr error: {}", msg)
}
}
}
}
impl std::error::Error for InterpolateError {}
impl From<numr::error::Error> for InterpolateError {
fn from(err: numr::error::Error) -> Self {
Self::NumrError(err.to_string())
}
}