1use thiserror::Error;
6
7#[derive(Error, Debug)]
11#[non_exhaustive]
12pub enum EmbedError {
13 #[error("model not loaded: {0}")]
15 ModelNotLoaded(String),
16
17 #[error("wrong model loaded: expected {expected}, got {actual}")]
22 WrongModelLoaded {
23 expected: String,
25 actual: String,
27 },
28
29 #[error("model initialization failed: {0}")]
31 ModelInitialization(String),
32
33 #[error("embedding inference failed: {0}")]
35 InferenceFailed(String),
36
37 #[error("task execution failed: {0}")]
41 TaskFailed(String),
42
43 #[error("invalid input: {0}")]
45 InvalidInput(String),
46
47 #[error("text too long: {length} bytes exceeds maximum {max} bytes")]
49 TextTooLong {
50 length: usize,
52 max: usize,
54 },
55
56 #[error("dimension mismatch: expected {expected}, got {actual}")]
58 DimensionMismatch {
59 expected: usize,
61 actual: usize,
63 },
64
65 #[error("model not supported: {0}")]
67 UnsupportedModel(String),
68
69 #[error("internal error: {0}")]
71 Internal(String),
72
73 #[error("tier mismatch in {op}: expected {expected:?}, got {actual:?}")]
76 TierMismatch {
77 op: &'static str,
79 expected: crate::simd::QuantizationTier,
81 actual: crate::simd::QuantizationTier,
83 },
84}
85
86pub type Result<T> = std::result::Result<T, EmbedError>;
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn test_error_display() {
95 let err = EmbedError::DimensionMismatch {
96 expected: 384,
97 actual: 768,
98 };
99 assert_eq!(err.to_string(), "dimension mismatch: expected 384, got 768");
100 }
101
102 #[test]
103 fn test_error_variants() {
104 let err = EmbedError::ModelNotLoaded("test".into());
105 assert_eq!(err.to_string(), "model not loaded: test");
106
107 let err = EmbedError::WrongModelLoaded {
108 expected: "small".into(),
109 actual: "large".into(),
110 };
111 assert!(err.to_string().contains("expected small"));
112
113 let err = EmbedError::ModelInitialization("failed".into());
114 assert!(err.to_string().contains("initialization"));
115
116 let err = EmbedError::InferenceFailed("oom".into());
117 assert!(err.to_string().contains("inference"));
118
119 let err = EmbedError::TaskFailed("panic".into());
120 assert!(err.to_string().contains("task"));
121
122 let err = EmbedError::InvalidInput("empty".into());
123 assert!(err.to_string().contains("invalid input"));
124
125 let err = EmbedError::UnsupportedModel("gpt4".into());
126 assert!(err.to_string().contains("not supported"));
127
128 let err = EmbedError::Internal("bug".into());
129 assert!(err.to_string().contains("internal"));
130
131 let err = EmbedError::TextTooLong {
132 length: 50000,
133 max: 32768,
134 };
135 assert!(err.to_string().contains("50000"));
136 assert!(err.to_string().contains("32768"));
137 }
138}