use crate::Scalar;
use core::fmt;
use core::ops::RangeInclusive;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum PathError<S> {
OutOfDomain {
param: S,
domain: RangeInclusive<S>,
},
NotDifferentiable {
param: S,
reason: &'static str,
},
Degenerate {
reason: &'static str,
},
Custom(&'static str),
}
impl<S: Scalar> PathError<S> {
pub fn out_of_domain(param: S, domain: RangeInclusive<S>) -> Self {
PathError::OutOfDomain { param, domain }
}
pub fn not_differentiable(param: S, reason: &'static str) -> Self {
PathError::NotDifferentiable { param, reason }
}
pub fn degenerate(reason: &'static str) -> Self {
PathError::Degenerate { reason }
}
}
impl<S: fmt::Debug> fmt::Display for PathError<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PathError::OutOfDomain { param, domain } => {
write!(
f,
"parameter {:?} is outside the valid domain {:?}",
param, domain
)
}
PathError::NotDifferentiable { param, reason } => {
write!(f, "path is not differentiable at {:?}: {}", param, reason)
}
PathError::Degenerate { reason } => {
write!(f, "path is degenerate: {}", reason)
}
PathError::Custom(msg) => write!(f, "{}", msg),
}
}
}
impl<S: fmt::Debug> core::error::Error for PathError<S> {}