Skip to main content

durability/
error.rs

1//! Error types for `durability`.
2
3use std::path::PathBuf;
4
5/// Result type for durability/persistence operations.
6pub type PersistenceResult<T> = Result<T, PersistenceError>;
7
8/// Errors returned by the `durability` crate.
9#[non_exhaustive]
10#[derive(thiserror::Error, Debug)]
11pub enum PersistenceError {
12    /// I/O error.
13    #[error("i/o error: {0}")]
14    Io(#[from] std::io::Error),
15
16    /// Format error (corrupt, unexpected, unsupported).
17    #[error("format error: {0}")]
18    Format(String),
19
20    /// Format error with optional expected/actual context.
21    ///
22    /// `expected`/`actual` exist to make “format mismatch” debugging concrete without
23    /// forcing every caller into bespoke error enums.
24    #[error("format error: {message}")]
25    FormatDetail {
26        /// Short, human-readable description of the mismatch.
27        message: String,
28        /// Optional “expected” value (stringified) for debugging.
29        expected: Option<String>,
30        /// Optional “actual” value (stringified) for debugging.
31        actual: Option<String>,
32    },
33
34    /// CRC mismatch (data corruption detected).
35    #[error("crc mismatch (expected {expected:#010x}, got {actual:#010x})")]
36    CrcMismatch {
37        /// CRC stored in the file/record header.
38        expected: u32,
39        /// CRC computed from the bytes that were read.
40        actual: u32,
41    },
42
43    /// Encoding error.
44    #[error("encode error: {0}")]
45    Encode(String),
46
47    /// Decoding error.
48    #[error("decode error: {0}")]
49    Decode(String),
50
51    /// Invalid state (operation not allowed in current state).
52    #[error("invalid state: {0}")]
53    InvalidState(String),
54
55    /// Invalid configuration.
56    #[error("invalid configuration: {0}")]
57    InvalidConfig(String),
58
59    /// Operation not supported.
60    #[error("operation not supported: {0}")]
61    NotSupported(String),
62
63    /// Lock acquisition failed (concurrent access conflict).
64    #[error("lock failed on {resource}: {reason}")]
65    LockFailed {
66        /// What we were trying to lock (file path, in-memory map, etc.).
67        resource: String,
68        /// Human-readable reason (poisoned lock, OS error, etc.).
69        reason: String,
70    },
71
72    /// Resource not found (file/segment/etc).
73    #[error("not found: {0}")]
74    NotFound(String),
75
76    /// Requested path does not exist.
77    #[error("missing path: {0}")]
78    MissingPath(PathBuf),
79}