use core::fmt::{self, Debug, Display};
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum RootFindingError<T> {
FailedToConverge { last_x: T, last_fx: T },
DivideByZero { last_x: T, last_fx: T },
InvalidBracket,
}
impl<T: Display> Display for RootFindingError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RootFindingError::FailedToConverge { last_x, last_fx } => {
write!(f, "Failed to converge. Last x: {}, Last f(x): {}", last_x, last_fx)
}
RootFindingError::DivideByZero { last_x, last_fx } => {
write!(
f,
"Attempted to divide by zero. Last x: {}, Last f(x): {}",
last_x, last_fx
)
}
RootFindingError::InvalidBracket => write!(f, "Invalid bracket provided."),
}
}
}
#[cfg(feature = "std")]
impl<T: Display + Debug> std::error::Error for RootFindingError<T> {}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum FinPrimError<T> {
DivideByZero,
RootFindingError(RootFindingError<T>),
}
impl<T: Display> Display for FinPrimError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FinPrimError::DivideByZero => write!(f, "Division by zero error."),
FinPrimError::RootFindingError(e) => write!(f, "Root finding error: {}", e),
}
}
}
#[cfg(feature = "std")]
impl<T: Display + Debug> std::error::Error for FinPrimError<T> {}