use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("configuration error: {0}")]
Configuration(String),
#[error("connection error: {0}")]
Connection(String),
#[error("vector adapter is not connected")]
NotConnected,
#[error("operation failed: {0}")]
Operation(String),
#[error("not found: {0}")]
NotFound(String),
#[error("namespace not found: {0}")]
NamespaceNotFound(String),
#[error("dimension mismatch: expected {expected}, got {actual}")]
DimensionMismatch { expected: usize, actual: usize },
#[error("missing dependency: {0}")]
MissingDependency(String),
#[error("backend error: {0}")]
Backend(String),
#[error(transparent)]
Other(#[from] Box<dyn std::error::Error + Send + Sync>),
}
impl Error {
pub fn config(msg: impl Into<String>) -> Self {
Self::Configuration(msg.into())
}
pub fn connection(msg: impl Into<String>) -> Self {
Self::Connection(msg.into())
}
pub fn operation(msg: impl Into<String>) -> Self {
Self::Operation(msg.into())
}
pub fn backend(msg: impl Into<String>) -> Self {
Self::Backend(msg.into())
}
pub fn dimension_mismatch(expected: usize, actual: usize) -> Self {
Self::DimensionMismatch { expected, actual }
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn configuration_error_message() {
let e = Error::config("dimension is required");
assert_eq!(e.to_string(), "configuration error: dimension is required");
}
#[test]
fn connection_error_message() {
let e = Error::connection("refused");
assert_eq!(e.to_string(), "connection error: refused");
}
#[test]
fn not_connected_message() {
assert_eq!(Error::NotConnected.to_string(), "vector adapter is not connected");
}
#[test]
fn operation_error_message() {
let e = Error::operation("upsert failed");
assert_eq!(e.to_string(), "operation failed: upsert failed");
}
#[test]
fn not_found_message() {
let e = Error::NotFound("rec-1".to_string());
assert_eq!(e.to_string(), "not found: rec-1");
}
#[test]
fn namespace_not_found_message() {
let e = Error::NamespaceNotFound("docs".to_string());
assert_eq!(e.to_string(), "namespace not found: docs");
}
#[test]
fn dimension_mismatch_message() {
let e = Error::dimension_mismatch(384, 512);
assert_eq!(e.to_string(), "dimension mismatch: expected 384, got 512");
}
#[test]
fn missing_dependency_message() {
let e = Error::MissingDependency("pgvector extension".to_string());
assert_eq!(e.to_string(), "missing dependency: pgvector extension");
}
#[test]
fn backend_error_message() {
let e = Error::backend("driver error");
assert_eq!(e.to_string(), "backend error: driver error");
}
#[test]
fn result_ok() {
let r: Result<i32> = Ok(42);
assert!(matches!(r, Ok(42)));
}
#[test]
fn result_err() {
let r: Result<i32> = Err(Error::NotConnected);
assert!(r.is_err());
}
}