use thiserror::Error;
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum MetadataError {
#[error("metadata key cannot be empty")]
EmptyKey,
#[error("metadata key too long: {length} bytes (max {max})")]
KeyTooLong {
length: usize,
max: usize,
},
#[error("invalid key format: '{key}' (must be alphanumeric + underscore)")]
InvalidKeyFormat {
key: String,
},
#[error("string value too long: {length} bytes (max {max})")]
StringValueTooLong {
length: usize,
max: usize,
},
#[error("string array too long: {length} elements (max {max})")]
ArrayTooLong {
length: usize,
max: usize,
},
#[error("invalid float value: {reason}")]
InvalidFloat {
reason: &'static str,
},
#[error("too many keys for vector {vector_id}: {count} (max {max})")]
TooManyKeys {
vector_id: u32,
count: usize,
max: usize,
},
#[error("vector {vector_id} not found")]
VectorNotFound {
vector_id: u32,
},
#[error("key '{key}' not found for vector {vector_id}")]
KeyNotFound {
vector_id: u32,
key: String,
},
#[error("serialization error: {0}")]
Serialization(String),
#[error("deserialization error: {0}")]
Deserialization(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_messages_are_descriptive() {
let error = MetadataError::EmptyKey;
assert!(error.to_string().contains("empty"));
let error = MetadataError::KeyTooLong {
length: 300,
max: 256,
};
assert!(error.to_string().contains("300"));
assert!(error.to_string().contains("256"));
let error = MetadataError::InvalidKeyFormat {
key: "bad-key".to_string(),
};
assert!(error.to_string().contains("bad-key"));
let error = MetadataError::StringValueTooLong {
length: 70000,
max: 65536,
};
assert!(error.to_string().contains("70000"));
let error = MetadataError::ArrayTooLong {
length: 2000,
max: 1024,
};
assert!(error.to_string().contains("2000"));
let error = MetadataError::InvalidFloat {
reason: "NaN not allowed",
};
assert!(error.to_string().contains("NaN"));
let error = MetadataError::TooManyKeys {
vector_id: 42,
count: 100,
max: 64,
};
assert!(error.to_string().contains("42"));
assert!(error.to_string().contains("100"));
let error = MetadataError::VectorNotFound { vector_id: 99 };
assert!(error.to_string().contains("99"));
let error = MetadataError::KeyNotFound {
vector_id: 5,
key: "missing_key".to_string(),
};
assert!(error.to_string().contains('5'));
assert!(error.to_string().contains("missing_key"));
let error = MetadataError::Serialization("failed".to_string());
assert!(error.to_string().contains("failed"));
let error = MetadataError::Deserialization("corrupt".to_string());
assert!(error.to_string().contains("corrupt"));
}
#[test]
fn test_error_equality() {
let e1 = MetadataError::EmptyKey;
let e2 = MetadataError::EmptyKey;
assert_eq!(e1, e2);
let e3 = MetadataError::KeyTooLong {
length: 100,
max: 50,
};
let e4 = MetadataError::KeyTooLong {
length: 100,
max: 50,
};
assert_eq!(e3, e4);
}
#[test]
fn test_error_clone() {
let error = MetadataError::InvalidKeyFormat {
key: "test".to_string(),
};
let cloned = error.clone();
assert_eq!(error, cloned);
}
#[test]
fn test_error_debug() {
let error = MetadataError::EmptyKey;
let debug_str = format!("{error:?}");
assert!(debug_str.contains("EmptyKey"));
}
}