1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Error, Debug)]
10pub enum Error {
11 #[error("empty document: {0}")]
13 EmptyDocument(String),
14
15 #[error("chunk exceeds maximum size: {size} > {max}")]
17 ChunkTooLarge {
18 size: usize,
20 max: usize,
22 },
23
24 #[error("embedding dimension mismatch: expected {expected}, got {actual}")]
26 DimensionMismatch {
27 expected: usize,
29 actual: usize,
31 },
32
33 #[error("index not found: {0}")]
35 IndexNotFound(String),
36
37 #[error("vector store error: {0}")]
39 VectorStore(String),
40
41 #[error("serialization error: {0}")]
43 Serialization(#[from] serde_json::Error),
44
45 #[error("serialization error: {0}")]
47 SerializationError(String),
48
49 #[error("IO error: {0}")]
51 Io(#[from] std::io::Error),
52
53 #[error("invalid configuration: {0}")]
55 InvalidConfig(String),
56
57 #[error("query error: {0}")]
59 Query(String),
60
61 #[error("embedding error: {0}")]
63 Embedding(String),
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_error_display_empty_document() {
72 let err = Error::EmptyDocument("test.txt".to_string());
73 assert_eq!(err.to_string(), "empty document: test.txt");
74 }
75
76 #[test]
77 fn test_error_display_chunk_too_large() {
78 let err = Error::ChunkTooLarge {
79 size: 1000,
80 max: 512,
81 };
82 assert_eq!(err.to_string(), "chunk exceeds maximum size: 1000 > 512");
83 }
84
85 #[test]
86 fn test_error_display_dimension_mismatch() {
87 let err = Error::DimensionMismatch {
88 expected: 384,
89 actual: 768,
90 };
91 assert_eq!(
92 err.to_string(),
93 "embedding dimension mismatch: expected 384, got 768"
94 );
95 }
96
97 #[test]
98 fn test_error_from_io() {
99 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
100 let err = Error::from(io_err);
101 assert!(err.to_string().contains("file not found"));
102 }
103
104 #[test]
105 fn test_result_type() {
106 fn may_fail(succeed: bool) -> Result<i32> {
107 if succeed {
108 Ok(42)
109 } else {
110 Err(Error::InvalidConfig("test".to_string()))
111 }
112 }
113
114 assert_eq!(may_fail(true).unwrap(), 42);
115 assert!(may_fail(false).is_err());
116 }
117}