1use crate::id::VectorId;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, thiserror::Error)]
10pub enum Error {
11 #[error("I/O error: {0}")]
13 Io(#[from] std::io::Error),
14
15 #[error("dimension mismatch: index expects {expected}, got {got}")]
17 DimensionMismatch {
18 expected: usize,
20 got: usize,
22 },
23
24 #[error("invalid configuration: {0}")]
26 InvalidConfig(String),
27
28 #[error("vector {0} not found")]
30 NotFound(VectorId),
31
32 #[error("corrupt index: {0}")]
34 Corrupt(String),
35
36 #[error("unsupported: {0}")]
38 Unsupported(String),
39}
40
41impl Error {
42 pub fn invalid_config(msg: impl Into<String>) -> Self {
44 Error::InvalidConfig(msg.into())
45 }
46
47 pub fn corrupt(msg: impl Into<String>) -> Self {
49 Error::Corrupt(msg.into())
50 }
51
52 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}