use crate::index::spec::IndexType;
use crate::value::Value;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum IndexError {
#[error("index already exists: {0}")]
AlreadyExists(String),
#[error("index not found: {0}")]
NotFound(String),
#[error("invalid index type: expected {expected:?}, got {got:?}")]
InvalidIndexType {
expected: IndexType,
got: IndexType,
},
#[error("unique constraint violation on index '{index_name}': value {value:?} already exists for element {existing_id}, cannot add element {new_id}")]
DuplicateValue {
index_name: String,
value: Value,
existing_id: u64,
new_id: u64,
},
#[error("index builder missing required property")]
MissingProperty,
#[error("value type not indexable: {0:?}")]
NotIndexable(Value),
#[error("internal index error: {0}")]
Internal(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_display_already_exists() {
let err = IndexError::AlreadyExists("idx_person_age".to_string());
assert_eq!(err.to_string(), "index already exists: idx_person_age");
}
#[test]
fn error_display_not_found() {
let err = IndexError::NotFound("idx_missing".to_string());
assert_eq!(err.to_string(), "index not found: idx_missing");
}
#[test]
fn error_display_duplicate_value() {
let err = IndexError::DuplicateValue {
index_name: "uniq_user_email".to_string(),
value: Value::String("alice@example.com".to_string()),
existing_id: 1,
new_id: 2,
};
let msg = err.to_string();
assert!(msg.contains("uniq_user_email"));
assert!(msg.contains("alice@example.com"));
assert!(msg.contains("1"));
assert!(msg.contains("2"));
}
#[test]
fn error_display_missing_property() {
let err = IndexError::MissingProperty;
assert_eq!(err.to_string(), "index builder missing required property");
}
#[test]
fn error_display_not_indexable() {
let err = IndexError::NotIndexable(Value::List(vec![Value::Int(1)]));
assert!(err.to_string().contains("not indexable"));
}
#[test]
fn error_display_invalid_index_type() {
let err = IndexError::InvalidIndexType {
expected: IndexType::BTree,
got: IndexType::Unique,
};
let msg = err.to_string();
assert!(msg.contains("invalid index type"));
assert!(msg.contains("BTree"));
assert!(msg.contains("Unique"));
}
}