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!(
91 err.to_string(),
92 "Dimension mismatch: expected 384, got 128"
93 );
94 }
95
96 #[test]
97 fn invalid_vector_display() {
98 let err = Error::InvalidVector("contains NaN".into());
99 assert_eq!(err.to_string(), "Invalid vector: contains NaN");
100 }
101
102 #[test]
103 fn numerical_instability_display() {
104 let err = Error::NumericalInstability("norm overflow".into());
105 assert_eq!(
106 err.to_string(),
107 "Numerical instability: norm overflow"
108 );
109 }
110
111 #[test]
112 fn index_display() {
113 let err = Error::Index("HNSW build failed".into());
114 assert_eq!(err.to_string(), "Index error: HNSW build failed");
115 }
116
117 #[test]
118 fn persistence_display() {
119 let err = Error::Persistence("disk full".into());
120 assert_eq!(err.to_string(), "Persistence error: disk full");
121 }
122
123 #[test]
124 fn configuration_display() {
125 let err = Error::Configuration("bad metric".into());
126 assert_eq!(err.to_string(), "Configuration error: bad metric");
127 }
128
129 #[test]
130 fn internal_display() {
131 let err = Error::Internal("unexpected state".into());
132 assert_eq!(err.to_string(), "Internal error: unexpected state");
133 }
134
135 #[test]
136 fn io_error_from_std_io_error() {
137 let io_err = IoError::new(ErrorKind::NotFound, "no such file");
138 let err: Error = io_err.into();
139 assert!(matches!(err, Error::Io(_)));
140 assert!(err.to_string().contains("no such file"));
141 }
142
143 #[test]
144 fn result_type_alias_accepts_ok() {
145 let ok: Result<i32> = Ok(7);
146 assert_eq!(ok.unwrap(), 7);
147 }
148
149 #[test]
150 fn result_type_alias_accepts_err() {
151 let err: Result<()> = Err(Error::CollectionNotFound("x".into()));
152 assert!(matches!(err, Err(Error::CollectionNotFound(name)) if name == "x"));
153 }
154
155 #[test]
156 fn debug_includes_variant_name() {
157 let err = Error::DimensionMismatch {
158 expected: 3,
159 actual: 2,
160 };
161 let dbg = format!("{err:?}");
162 assert!(dbg.contains("DimensionMismatch"));
163 assert!(dbg.contains("expected: 3"));
164 assert!(dbg.contains("actual: 2"));
165 }
166}