use core::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Error {
EmptyInput,
DimensionMismatch {
expected: usize,
found: usize,
},
ShapeMismatch {
expected: String,
actual: String,
},
InversionFailed,
InvalidClusterCount {
requested: usize,
n_items: usize,
},
ConvergenceFailure {
iterations: usize,
},
InvalidParameter {
name: &'static str,
message: &'static str,
},
DisconnectedGraph,
ConstraintViolation(String),
Other(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::EmptyInput => write!(f, "empty input provided"),
Error::DimensionMismatch { expected, found } => {
write!(f, "dimension mismatch: expected {expected}, found {found}")
}
Error::ShapeMismatch { expected, actual } => {
write!(f, "shape mismatch: expected {expected}, actual {actual}")
}
Error::InversionFailed => write!(f, "matrix inversion failed"),
Error::InvalidClusterCount { requested, n_items } => {
write!(f, "cannot create {requested} clusters from {n_items} items")
}
Error::ConvergenceFailure { iterations } => {
write!(f, "did not converge after {iterations} iterations")
}
Error::InvalidParameter { name, message } => {
write!(f, "invalid parameter '{name}': {message}")
}
Error::DisconnectedGraph => write!(f, "graph is disconnected"),
Error::ConstraintViolation(msg) => write!(f, "constraint violation: {msg}"),
Error::Other(msg) => write!(f, "{msg}"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(feature = "cluster")]
impl From<clump::Error> for Error {
fn from(e: clump::Error) -> Self {
match e {
clump::Error::EmptyInput => Error::EmptyInput,
clump::Error::InvalidParameter { name, message } => {
Error::InvalidParameter { name, message }
}
clump::Error::InvalidClusterCount { requested, n_items } => {
Error::InvalidClusterCount { requested, n_items }
}
clump::Error::DimensionMismatch { expected, found } => {
Error::DimensionMismatch { expected, found }
}
clump::Error::ConstraintViolation(msg) => Error::ConstraintViolation(msg),
clump::Error::Other(msg) => Error::Other(msg),
}
}
}