use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum EmbedError {
#[error("model not loaded: {0}")]
ModelNotLoaded(String),
#[error("wrong model loaded: expected {expected}, got {actual}")]
WrongModelLoaded {
expected: String,
actual: String,
},
#[error("model initialization failed: {0}")]
ModelInitialization(String),
#[error("embedding inference failed: {0}")]
InferenceFailed(String),
#[error("task execution failed: {0}")]
TaskFailed(String),
#[error("invalid input: {0}")]
InvalidInput(String),
#[error("text too long: {length} chars exceeds maximum {max} chars")]
TextTooLong {
length: usize,
max: usize,
},
#[error("dimension mismatch: expected {expected}, got {actual}")]
DimensionMismatch {
expected: usize,
actual: usize,
},
#[error("model not supported: {0}")]
UnsupportedModel(String),
#[error("internal error: {0}")]
Internal(String),
}
pub type Result<T> = std::result::Result<T, EmbedError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = EmbedError::DimensionMismatch {
expected: 384,
actual: 768,
};
assert_eq!(err.to_string(), "dimension mismatch: expected 384, got 768");
}
#[test]
fn test_error_variants() {
let err = EmbedError::ModelNotLoaded("test".into());
assert_eq!(err.to_string(), "model not loaded: test");
let err = EmbedError::WrongModelLoaded {
expected: "small".into(),
actual: "large".into(),
};
assert!(err.to_string().contains("expected small"));
let err = EmbedError::ModelInitialization("failed".into());
assert!(err.to_string().contains("initialization"));
let err = EmbedError::InferenceFailed("oom".into());
assert!(err.to_string().contains("inference"));
let err = EmbedError::TaskFailed("panic".into());
assert!(err.to_string().contains("task"));
let err = EmbedError::InvalidInput("empty".into());
assert!(err.to_string().contains("invalid input"));
let err = EmbedError::UnsupportedModel("gpt4".into());
assert!(err.to_string().contains("not supported"));
let err = EmbedError::Internal("bug".into());
assert!(err.to_string().contains("internal"));
let err = EmbedError::TextTooLong {
length: 50000,
max: 32768,
};
assert!(err.to_string().contains("50000"));
assert!(err.to_string().contains("32768"));
}
}