use thiserror::Error;
#[derive(Debug, Error)]
pub enum PersistError {
#[error("database error: {0}")]
DatabaseError(#[from] noxu_db::NoxuError),
#[error("serialization error: {0}")]
SerializationError(String),
#[error("store not open")]
StoreNotOpen,
#[error("index not available: {0}")]
IndexNotAvailable(String),
#[error(
"DPL secondary indexes are in-memory only in v1.5; secondary \
updates are not atomic with the user transaction (see \
docs/src/collections/entity-persistence.md, v1.5 limitations)"
)]
SecondariesNotTransactional,
}
pub type Result<T> = std::result::Result<T, PersistError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialization_error_display() {
let err = PersistError::SerializationError("bad format".to_string());
assert_eq!(err.to_string(), "serialization error: bad format");
}
#[test]
fn test_store_not_open_display() {
let err = PersistError::StoreNotOpen;
assert_eq!(err.to_string(), "store not open");
}
#[test]
fn test_index_not_available_display() {
let err = PersistError::IndexNotAvailable("email_idx".to_string());
assert_eq!(err.to_string(), "index not available: email_idx");
}
#[test]
fn test_secondaries_not_transactional_display() {
let err = PersistError::SecondariesNotTransactional;
let msg = err.to_string();
assert!(msg.contains("secondary indexes"));
assert!(msg.contains("in-memory"));
}
#[test]
fn test_database_error_from() {
let db_err = noxu_db::NoxuError::DatabaseClosed;
let err: PersistError = db_err.into();
assert!(matches!(err, PersistError::DatabaseError(_)));
assert!(err.to_string().contains("database error"));
}
}