use std::{convert::Infallible, error::Error, fmt};
use crate::FitError;
#[derive(Debug)]
pub enum CrossValidationError<E = Infallible> {
InvalidInput {
source: FitError<E>,
},
Fold {
fold: usize,
source: FitError<E>,
},
NonConverged {
fold: Option<usize>,
candidate: usize,
},
Refit {
source: FitError<E>,
},
}
impl<E> From<FitError<E>> for CrossValidationError<E> {
fn from(source: FitError<E>) -> Self {
Self::InvalidInput { source }
}
}
impl<E: fmt::Display> fmt::Display for CrossValidationError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidInput { source } => write!(f, "invalid cross-validation input: {source}"),
Self::Fold { fold, source } => {
write!(f, "cross-validation fold {fold} failed: {source}")
}
Self::NonConverged {
fold: Some(fold),
candidate,
} => write!(
f,
"cross-validation fold {fold}, candidate {candidate} did not converge"
),
Self::NonConverged {
fold: None,
candidate,
} => write!(f, "full-data refit candidate {candidate} did not converge"),
Self::Refit { source } => {
write!(f, "cross-validation full-data refit failed: {source}")
}
}
}
}
impl<E: Error + 'static> Error for CrossValidationError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::InvalidInput { source } | Self::Fold { source, .. } | Self::Refit { source } => {
Some(source)
}
Self::NonConverged { .. } => None,
}
}
}