Skip to main content

aimdb_persistence/
error.rs

1//! Persistence error types for AimDB.
2
3use core::fmt;
4
5/// Errors that can occur during persistence operations.
6#[derive(Debug)]
7pub enum PersistenceError {
8    /// The persistence backend is not configured.
9    /// Returned when calling `.query_latest()` / `.query_range()` on an `AimDb`
10    /// that was built without `.with_persistence()`.
11    NotConfigured,
12
13    /// The backend writer thread has shut down (all senders dropped).
14    BackendShutdown,
15
16    /// A backend-specific error (e.g. SQLite, Postgres).
17    Backend(String),
18
19    /// JSON serialization/deserialization error.
20    Serialization(String),
21}
22
23impl fmt::Display for PersistenceError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            PersistenceError::NotConfigured => {
27                write!(
28                    f,
29                    "persistence backend not configured (call .with_persistence() on the builder)"
30                )
31            }
32            PersistenceError::BackendShutdown => {
33                write!(f, "persistence backend has shut down")
34            }
35            PersistenceError::Backend(msg) => write!(f, "persistence backend error: {}", msg),
36            PersistenceError::Serialization(msg) => {
37                write!(f, "persistence serialization error: {}", msg)
38            }
39        }
40    }
41}
42
43impl std::error::Error for PersistenceError {}
44
45impl From<serde_json::Error> for PersistenceError {
46    fn from(e: serde_json::Error) -> Self {
47        PersistenceError::Serialization(e.to_string())
48    }
49}