nectar_primitives/error.rs
1//! Error types for the nectar-primitives crate
2//!
3//! This module provides error types and helper functions for handling
4//! errors that occur in various components of the crate.
5//!
6//! ## Error Structure
7//!
8//! The crate uses a two-level error hierarchy:
9//!
10//! - `PrimitivesError`: The top-level error type that wraps all other errors
11//! - Component-specific errors: More detailed errors from specific subsystems
12//! (like `BmtError` and `ChunkError`)
13//!
14//! ## Example Usage
15//!
16//! ```
17//! use nectar_primitives::error::{PrimitivesError, Result};
18//!
19//! fn fallible_operation() -> Result<()> {
20//! // Something that might fail
21//! Ok(())
22//! }
23//!
24//! fn handle_errors() {
25//! match fallible_operation() {
26//! Ok(_) => println!("Operation succeeded"),
27//! Err(e) => match e {
28//! PrimitivesError::Bmt(bmt_err) => println!("BMT error: {}", bmt_err),
29//! PrimitivesError::Chunk(chunk_err) => println!("Chunk error: {}", chunk_err),
30//! _ => println!("Other error: {}", e),
31//! }
32//! }
33//! }
34//! ```
35//!
36//! This design allows for detailed error reporting while maintaining a consistent
37//! interface across the crate.
38
39use thiserror::Error;
40
41/// Result type for operations in the primitives crate
42pub type Result<T> = std::result::Result<T, PrimitivesError>;
43
44/// Main error type for the primitives crate
45///
46/// This enum represents all the possible errors that can occur when using
47/// the nectar-primitives crate. It wraps component-specific errors like
48/// `BmtError` and `ChunkError` to provide a unified error interface.
49#[non_exhaustive]
50#[derive(Error, Debug)]
51pub enum PrimitivesError {
52 /// Errors from BMT operations
53 #[error(transparent)]
54 Bmt(#[from] crate::bmt::error::BmtError),
55
56 /// Errors from chunk operations
57 #[error(transparent)]
58 Chunk(#[from] crate::chunk::error::ChunkError),
59
60 /// Errors from file operations
61 #[error(transparent)]
62 File(#[from] crate::file::error::FileError),
63
64 /// Errors from chunk store operations
65 #[error(transparent)]
66 Store(#[from] crate::store::ChunkStoreError),
67
68 /// Errors from encryption operations
69 #[error(transparent)]
70 Encryption(#[from] crate::chunk::encryption::EncryptionError),
71
72 /// Input/output errors
73 #[error("I/O error: {0}")]
74 Io(#[from] std::io::Error),
75
76 /// Array conversion errors
77 #[error("Array conversion error: {0}")]
78 ArrayConversion(#[from] std::array::TryFromSliceError),
79}