use core::fmt;
use error_forge::ForgeError;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IqdbError {
DimensionMismatch {
expected: usize,
found: usize,
},
InvalidVector,
InvalidConfig {
reason: &'static str,
},
NotFound,
Duplicate,
InvalidMetric,
InvalidFilter,
ResourceLimitExceeded {
kind: &'static str,
max: usize,
found: usize,
},
}
impl fmt::Display for IqdbError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DimensionMismatch { expected, found } => {
write!(
f,
"vector dimension mismatch: expected {expected}, found {found}"
)
}
Self::InvalidVector => f.write_str("invalid vector"),
Self::InvalidConfig { reason } => write!(f, "invalid configuration: {reason}"),
Self::NotFound => f.write_str("not found"),
Self::Duplicate => f.write_str("duplicate id"),
Self::InvalidMetric => f.write_str("invalid distance metric"),
Self::InvalidFilter => f.write_str("invalid filter"),
Self::ResourceLimitExceeded { kind, max, found } => {
write!(f, "resource limit exceeded: {kind} max={max} found={found}")
}
}
}
}
impl std::error::Error for IqdbError {}
impl ForgeError for IqdbError {
#[inline]
fn kind(&self) -> &'static str {
match self {
Self::DimensionMismatch { .. } => "DimensionMismatch",
Self::InvalidVector => "InvalidVector",
Self::InvalidConfig { .. } => "InvalidConfig",
Self::NotFound => "NotFound",
Self::Duplicate => "Duplicate",
Self::InvalidMetric => "InvalidMetric",
Self::InvalidFilter => "InvalidFilter",
Self::ResourceLimitExceeded { .. } => "ResourceLimitExceeded",
}
}
#[inline]
fn caption(&self) -> &'static str {
match self {
Self::DimensionMismatch { .. } => "vector dimension does not match the index",
Self::InvalidVector => "vector is empty or contains non-finite components",
Self::InvalidConfig { .. } => "configuration is not valid for the requested operation",
Self::NotFound => "requested id is not present in the index",
Self::Duplicate => "id already exists in the index",
Self::InvalidMetric => "distance metric does not match the index or vectors",
Self::InvalidFilter => "filter expression is malformed or cannot be evaluated",
Self::ResourceLimitExceeded { .. } => "an input exceeded a configured resource limit",
}
}
}
pub type Result<T> = core::result::Result<T, IqdbError>;