Skip to main content

rig_memvid/
error.rs

1//! Error types for `rig-memvid`.
2
3use rig::vector_store::{VectorStoreError, request::FilterError};
4
5/// Errors produced by `rig-memvid`.
6#[derive(Debug, thiserror::Error)]
7pub enum MemvidError {
8    /// Underlying memvid-core failure.
9    #[error("memvid error: {0}")]
10    Memvid(#[from] memvid_core::MemvidError),
11
12    /// I/O error while opening, creating, or locking a memvid file.
13    #[error("i/o error: {0}")]
14    Io(#[from] std::io::Error),
15
16    /// Serde failure while round-tripping a hit into the caller's document
17    /// type, or while encoding metadata into JSON for storage.
18    #[error("serialization error: {0}")]
19    Serde(#[from] serde_json::Error),
20
21    /// A filter clause was constructed that memvid cannot represent (for
22    /// example a `gt`/`lt` predicate, an `or` combination, or an `eq` against
23    /// an unknown key).
24    #[error("unsupported filter clause: {0}")]
25    UnsupportedFilter(String),
26
27    /// The store's mutex was poisoned by a previous panic.
28    #[error("memvid store mutex poisoned")]
29    Poisoned,
30}
31
32impl From<MemvidError> for VectorStoreError {
33    /// Map a [`MemvidError`] onto the closest [`VectorStoreError`] variant
34    /// so rig consumers can inspect failures without downcasting through
35    /// the generic `DatastoreError` boxed trait object.
36    ///
37    /// Note: [`MemvidError::Memvid`], [`MemvidError::Io`], and
38    /// [`MemvidError::Poisoned`] all collapse into
39    /// [`VectorStoreError::DatastoreError`]; downstream callers that need
40    /// to distinguish them should match on the boxed source via
41    /// `Error::downcast_ref::<MemvidError>()`.
42    fn from(err: MemvidError) -> Self {
43        match err {
44            MemvidError::Serde(e) => VectorStoreError::JsonError(e),
45            MemvidError::UnsupportedFilter(msg) => {
46                VectorStoreError::FilterError(FilterError::TypeError(msg))
47            }
48            other => VectorStoreError::DatastoreError(Box::new(other)),
49        }
50    }
51}