1use thiserror::Error;
4
5#[derive(Error, Debug)]
9#[non_exhaustive]
10pub enum EmbedError {
11 #[error("model not loaded: {0}")]
13 ModelNotLoaded(String),
14
15 #[error("wrong model loaded: expected {expected}, got {actual}")]
20 WrongModelLoaded {
21 expected: String,
23 actual: String,
25 },
26
27 #[error("model initialization failed: {0}")]
29 ModelInitialization(String),
30
31 #[error("embedding inference failed: {0}")]
33 InferenceFailed(String),
34
35 #[error("task execution failed: {0}")]
39 TaskFailed(String),
40
41 #[error("invalid input: {0}")]
43 InvalidInput(String),
44
45 #[error("text too long: {length} chars exceeds maximum {max} chars")]
47 TextTooLong {
48 length: usize,
50 max: usize,
52 },
53
54 #[error("dimension mismatch: expected {expected}, got {actual}")]
56 DimensionMismatch {
57 expected: usize,
59 actual: usize,
61 },
62
63 #[error("model not supported: {0}")]
65 UnsupportedModel(String),
66
67 #[error("internal error: {0}")]
69 Internal(String),
70
71 #[error("tier mismatch in {op}: expected {expected:?}, got {actual:?}")]
77 TierMismatch {
78 op: &'static str,
80 expected: crate::simd::QuantizationTier,
82 actual: crate::simd::QuantizationTier,
84 },
85}
86
87pub type Result<T> = std::result::Result<T, EmbedError>;
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn test_error_display() {
96 let err = EmbedError::DimensionMismatch {
97 expected: 384,
98 actual: 768,
99 };
100 assert_eq!(err.to_string(), "dimension mismatch: expected 384, got 768");
101 }
102
103 #[test]
104 fn test_error_variants() {
105 let err = EmbedError::ModelNotLoaded("test".into());
106 assert_eq!(err.to_string(), "model not loaded: test");
107
108 let err = EmbedError::WrongModelLoaded {
109 expected: "small".into(),
110 actual: "large".into(),
111 };
112 assert!(err.to_string().contains("expected small"));
113
114 let err = EmbedError::ModelInitialization("failed".into());
115 assert!(err.to_string().contains("initialization"));
116
117 let err = EmbedError::InferenceFailed("oom".into());
118 assert!(err.to_string().contains("inference"));
119
120 let err = EmbedError::TaskFailed("panic".into());
121 assert!(err.to_string().contains("task"));
122
123 let err = EmbedError::InvalidInput("empty".into());
124 assert!(err.to_string().contains("invalid input"));
125
126 let err = EmbedError::UnsupportedModel("gpt4".into());
127 assert!(err.to_string().contains("not supported"));
128
129 let err = EmbedError::Internal("bug".into());
130 assert!(err.to_string().contains("internal"));
131
132 let err = EmbedError::TextTooLong {
133 length: 50000,
134 max: 32768,
135 };
136 assert!(err.to_string().contains("50000"));
137 assert!(err.to_string().contains("32768"));
138 }
139}