use std::error::Error;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum AddError {
DimMismatch { existing: usize, got: usize },
DimNotMultipleOf8(usize),
VectorBufferNotMultipleOfDim { vectors_len: usize, dim: usize },
IdsCountMismatch { expected: usize, got: usize },
IdAlreadyPresent(u64),
InvalidInputValue {
vector_index: usize,
coord_index: usize,
value: f32,
},
}
impl fmt::Display for AddError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DimMismatch { existing, got } => {
write!(f, "dim mismatch: index dim={existing}, batch dim={got}")
}
Self::DimNotMultipleOf8(dim) => {
write!(f, "dim must be a multiple of 8, got {dim}")
}
Self::VectorBufferNotMultipleOfDim { vectors_len, dim } => write!(
f,
"vector buffer length {vectors_len} not a multiple of dim {dim}",
),
Self::IdsCountMismatch { expected, got } => {
write!(f, "expected {expected} ids, got {got}")
}
Self::IdAlreadyPresent(id) => {
write!(f, "id {id} already present in index")
}
Self::InvalidInputValue {
vector_index,
coord_index,
value,
} => write!(
f,
"invalid input value at vector {vector_index}, coord {coord_index}: {value} \
(must be finite and |value| < 1e16 to avoid f32 norm overflow)",
),
}
}
}
impl Error for AddError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConstructError {
BitWidthOutOfRange(usize),
DimNotPositiveMultipleOf8(usize),
}
impl fmt::Display for ConstructError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BitWidthOutOfRange(bw) => {
write!(f, "bit_width must be 2, 3, or 4, got {bw}")
}
Self::DimNotPositiveMultipleOf8(dim) => {
write!(f, "dim must be a positive multiple of 8, got {dim}")
}
}
}
}
impl Error for ConstructError {}