1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Error, Debug)]
10pub enum Error {
11 #[error("Collection '{0}' already exists")]
13 CollectionExists(String),
14
15 #[error("Collection '{0}' not found")]
17 CollectionNotFound(String),
18
19 #[error("Vector '{0}' not found")]
21 VectorNotFound(String),
22
23 #[error("Dimension mismatch: expected {expected}, got {actual}")]
25 DimensionMismatch {
26 expected: usize,
28 actual: usize,
30 },
31
32 #[error("Invalid vector: {0}")]
34 InvalidVector(String),
35
36 #[error("Numerical instability: {0}")]
38 NumericalInstability(String),
39
40 #[error("Index error: {0}")]
42 Index(String),
43
44 #[error("Persistence error: {0}")]
46 Persistence(String),
47
48 #[error("Configuration error: {0}")]
50 Configuration(String),
51
52 #[error("I/O error: {0}")]
54 Io(#[from] std::io::Error),
55
56 #[error("Internal error: {0}")]
58 Internal(String),
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use std::io::{Error as IoError, ErrorKind};
65
66 #[test]
67 fn collection_exists_display() {
68 let err = Error::CollectionExists("docs".into());
69 assert_eq!(err.to_string(), "Collection 'docs' already exists");
70 }
71
72 #[test]
73 fn collection_not_found_display() {
74 let err = Error::CollectionNotFound("missing".into());
75 assert_eq!(err.to_string(), "Collection 'missing' not found");
76 }
77
78 #[test]
79 fn vector_not_found_display() {
80 let err = Error::VectorNotFound("vec-42".into());
81 assert_eq!(err.to_string(), "Vector 'vec-42' not found");
82 }
83
84 #[test]
85 fn dimension_mismatch_display() {
86 let err = Error::DimensionMismatch {
87 expected: 384,
88 actual: 128,
89 };
90 assert_eq!(err.to_string(), "Dimension mismatch: expected 384, got 128");
91 }
92
93 #[test]
94 fn invalid_vector_display() {
95 let err = Error::InvalidVector("contains NaN".into());
96 assert_eq!(err.to_string(), "Invalid vector: contains NaN");
97 }
98
99 #[test]
100 fn numerical_instability_display() {
101 let err = Error::NumericalInstability("norm overflow".into());
102 assert_eq!(err.to_string(), "Numerical instability: norm overflow");
103 }
104
105 #[test]
106 fn index_display() {
107 let err = Error::Index("HNSW build failed".into());
108 assert_eq!(err.to_string(), "Index error: HNSW build failed");
109 }
110
111 #[test]
112 fn persistence_display() {
113 let err = Error::Persistence("disk full".into());
114 assert_eq!(err.to_string(), "Persistence error: disk full");
115 }
116
117 #[test]
118 fn configuration_display() {
119 let err = Error::Configuration("bad metric".into());
120 assert_eq!(err.to_string(), "Configuration error: bad metric");
121 }
122
123 #[test]
124 fn internal_display() {
125 let err = Error::Internal("unexpected state".into());
126 assert_eq!(err.to_string(), "Internal error: unexpected state");
127 }
128
129 #[test]
130 fn io_error_from_std_io_error() {
131 let io_err = IoError::new(ErrorKind::NotFound, "no such file");
132 let err: Error = io_err.into();
133 assert!(matches!(err, Error::Io(_)));
134 assert!(err.to_string().contains("no such file"));
135 }
136
137 #[test]
138 fn result_type_alias_accepts_ok() {
139 let value = 7;
140 let ok: Result<i32> = Ok(value);
141 assert!(matches!(ok, Ok(got) if got == value));
142 }
143
144 #[test]
145 fn result_type_alias_accepts_err() {
146 let err: Result<()> = Err(Error::CollectionNotFound("x".into()));
147 assert!(matches!(err, Err(Error::CollectionNotFound(name)) if name == "x"));
148 }
149
150 #[test]
151 fn debug_includes_variant_name() {
152 let err = Error::DimensionMismatch {
153 expected: 3,
154 actual: 2,
155 };
156 let dbg = format!("{err:?}");
157 assert!(dbg.contains("DimensionMismatch"));
158 assert!(dbg.contains("expected: 3"));
159 assert!(dbg.contains("actual: 2"));
160 }
161}