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
72pub type Result<T> = std::result::Result<T, EmbedError>;
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_error_display() {
81 let err = EmbedError::DimensionMismatch {
82 expected: 384,
83 actual: 768,
84 };
85 assert_eq!(err.to_string(), "dimension mismatch: expected 384, got 768");
86 }
87
88 #[test]
89 fn test_error_variants() {
90 let err = EmbedError::ModelNotLoaded("test".into());
91 assert_eq!(err.to_string(), "model not loaded: test");
92
93 let err = EmbedError::WrongModelLoaded {
94 expected: "small".into(),
95 actual: "large".into(),
96 };
97 assert!(err.to_string().contains("expected small"));
98
99 let err = EmbedError::ModelInitialization("failed".into());
100 assert!(err.to_string().contains("initialization"));
101
102 let err = EmbedError::InferenceFailed("oom".into());
103 assert!(err.to_string().contains("inference"));
104
105 let err = EmbedError::TaskFailed("panic".into());
106 assert!(err.to_string().contains("task"));
107
108 let err = EmbedError::InvalidInput("empty".into());
109 assert!(err.to_string().contains("invalid input"));
110
111 let err = EmbedError::UnsupportedModel("gpt4".into());
112 assert!(err.to_string().contains("not supported"));
113
114 let err = EmbedError::Internal("bug".into());
115 assert!(err.to_string().contains("internal"));
116
117 let err = EmbedError::TextTooLong {
118 length: 50000,
119 max: 32768,
120 };
121 assert!(err.to_string().contains("50000"));
122 assert!(err.to_string().contains("32768"));
123 }
124}