Skip to main content

slate_core/
error.rs

1//! Central error type for the Slate-ANN engine.
2
3use crate::id::VectorId;
4
5/// Result alias used throughout Slate-ANN.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors produced by the Slate-ANN engine.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// An underlying I/O operation failed (file open, read, mmap, etc.).
12    #[error("I/O error: {0}")]
13    Io(#[from] std::io::Error),
14
15    /// A vector's dimensionality did not match the index.
16    #[error("dimension mismatch: index expects {expected}, got {got}")]
17    DimensionMismatch {
18        /// Dimensionality the index was built with.
19        expected: usize,
20        /// Dimensionality of the offending vector.
21        got: usize,
22    },
23
24    /// A configuration value was invalid or self-inconsistent.
25    #[error("invalid configuration: {0}")]
26    InvalidConfig(String),
27
28    /// A requested vector id is not present in the index.
29    #[error("vector {0} not found")]
30    NotFound(VectorId),
31
32    /// An on-disk structure failed validation (bad magic, version, or length).
33    #[error("corrupt index: {0}")]
34    Corrupt(String),
35
36    /// A requested operation or combination of options is not supported.
37    #[error("unsupported: {0}")]
38    Unsupported(String),
39}
40
41impl Error {
42    /// Convenience constructor for [`Error::InvalidConfig`].
43    pub fn invalid_config(msg: impl Into<String>) -> Self {
44        Error::InvalidConfig(msg.into())
45    }
46
47    /// Convenience constructor for [`Error::Corrupt`].
48    pub fn corrupt(msg: impl Into<String>) -> Self {
49        Error::Corrupt(msg.into())
50    }
51
52    /// Convenience constructor for [`Error::Unsupported`].
53    pub fn unsupported(msg: impl Into<String>) -> Self {
54        Error::Unsupported(msg.into())
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn messages_render() {
64        let e = Error::DimensionMismatch {
65            expected: 768,
66            got: 512,
67        };
68        assert_eq!(e.to_string(), "dimension mismatch: index expects 768, got 512");
69
70        let e = Error::NotFound(VectorId::new(9));
71        assert_eq!(e.to_string(), "vector #9 not found");
72    }
73
74    #[test]
75    fn io_errors_convert() {
76        let io = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
77        let e: Error = io.into();
78        assert!(matches!(e, Error::Io(_)));
79    }
80}