use crate::id::VectorId;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("dimension mismatch: index expects {expected}, got {got}")]
DimensionMismatch {
expected: usize,
got: usize,
},
#[error("invalid configuration: {0}")]
InvalidConfig(String),
#[error("vector {0} not found")]
NotFound(VectorId),
#[error("corrupt index: {0}")]
Corrupt(String),
#[error("unsupported: {0}")]
Unsupported(String),
}
impl Error {
pub fn invalid_config(msg: impl Into<String>) -> Self {
Error::InvalidConfig(msg.into())
}
pub fn corrupt(msg: impl Into<String>) -> Self {
Error::Corrupt(msg.into())
}
pub fn unsupported(msg: impl Into<String>) -> Self {
Error::Unsupported(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn messages_render() {
let e = Error::DimensionMismatch {
expected: 768,
got: 512,
};
assert_eq!(e.to_string(), "dimension mismatch: index expects 768, got 512");
let e = Error::NotFound(VectorId::new(9));
assert_eq!(e.to_string(), "vector #9 not found");
}
#[test]
fn io_errors_convert() {
let io = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
let e: Error = io.into();
assert!(matches!(e, Error::Io(_)));
}
}