Skip to main content

noxu_collections/
error.rs

1//! Error types for the collections crate.
2//!
3
4use thiserror::Error;
5
6/// Errors that can occur when using collection views.
7///
8#[derive(Debug, Error)]
9pub enum CollectionError {
10    /// An underlying database error occurred.
11    #[error("database error: {0}")]
12    DatabaseError(#[from] noxu_db::NoxuError),
13
14    /// A binding conversion error occurred.
15    #[error("binding error: {0}")]
16    BindingError(String),
17
18    /// The iterator has been exhausted.
19    #[error("iterator exhausted")]
20    IteratorExhausted,
21
22    /// The collection is read-only and a write was attempted.
23    #[error("collection is read-only")]
24    ReadOnly,
25
26    /// A concurrent modification was detected.
27    #[error("concurrent modification detected")]
28    ConcurrentModification,
29
30    /// An illegal state was encountered.
31    #[error("illegal state: {0}")]
32    IllegalState(String),
33}
34
35/// Result type for collection operations.
36pub type Result<T> = std::result::Result<T, CollectionError>;
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_error_display() {
44        let err = CollectionError::ReadOnly;
45        assert_eq!(err.to_string(), "collection is read-only");
46    }
47
48    #[test]
49    fn test_database_error_conversion() {
50        let db_err = noxu_db::NoxuError::DatabaseClosed;
51        let err: CollectionError = db_err.into();
52        assert!(matches!(err, CollectionError::DatabaseError(_)));
53        assert!(err.to_string().contains("database"));
54    }
55
56    #[test]
57    fn test_binding_error() {
58        let err = CollectionError::BindingError("bad conversion".to_string());
59        assert!(err.to_string().contains("binding error"));
60    }
61
62    #[test]
63    fn test_iterator_exhausted() {
64        let err = CollectionError::IteratorExhausted;
65        assert_eq!(err.to_string(), "iterator exhausted");
66    }
67
68    #[test]
69    fn test_illegal_state() {
70        let err =
71            CollectionError::IllegalState("cursor not positioned".to_string());
72        assert!(err.to_string().contains("illegal state"));
73    }
74}