use alloc::string::String;
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("invalid input: {0}")]
InvalidInput(String),
#[error("model id mismatch: a = {a}, b = {b}")]
ModelMismatch {
a: String,
b: String,
},
#[error("dimension mismatch: a = {a}, b = {b}")]
DimensionMismatch {
a: usize,
b: usize,
},
#[error("config error: {0}")]
Config(String),
#[cfg(feature = "std")]
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[cfg(feature = "semantic")]
#[error("tokenizer error: {0}")]
Tokenizer(String),
#[cfg(feature = "semantic")]
#[error("onnx error: {0}")]
Onnx(String),
#[cfg(any(feature = "openai", feature = "voyage", feature = "cohere"))]
#[error("http error: {0}")]
Http(String),
#[cfg(feature = "semantic")]
#[error("provider returned no embeddings")]
EmptyEmbedding,
#[error("schema version mismatch: expected {expected}, got {actual}")]
SchemaMismatch {
expected: u16,
actual: u16,
},
#[error("feature `{0}` not enabled at compile time")]
FeatureDisabled(&'static str),
}
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn invalid_input_renders_payload() {
let s = Error::InvalidInput("empty document".into()).to_string();
assert!(s.contains("empty document"), "got: {s}");
}
#[test]
fn model_mismatch_renders_both_ids() {
let s = Error::ModelMismatch {
a: "bge-small".into(),
b: "bge-large".into(),
}
.to_string();
assert!(s.contains("bge-small"));
assert!(s.contains("bge-large"));
}
#[test]
fn dimension_mismatch_renders_both_dims() {
let s = Error::DimensionMismatch { a: 384, b: 768 }.to_string();
assert!(s.contains("384"));
assert!(s.contains("768"));
}
#[test]
fn schema_mismatch_renders_versions() {
let s = Error::SchemaMismatch {
expected: 1,
actual: 7,
}
.to_string();
assert!(s.contains('1'));
assert!(s.contains('7'));
}
#[test]
fn feature_disabled_renders_name() {
let s = Error::FeatureDisabled("semantic").to_string();
assert!(s.contains("semantic"));
}
#[test]
fn error_is_send_sync_static() {
fn assert_traits<T: Send + Sync + 'static>() {}
assert_traits::<Error>();
}
#[test]
fn result_alias_works() {
let f = |x: u32| -> Result<u32> { Ok(x * 2) };
assert_eq!(f(21).unwrap(), 42);
}
}