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#[derive(Error, Debug)]
50pub enum PrimitivesError {
51 /// Errors from BMT operations
52 #[error(transparent)]
53 Bmt(#[from] crate::bmt::error::BmtError),
54
55 /// Errors from chunk operations
56 #[error(transparent)]
57 Chunk(#[from] crate::chunk::error::ChunkError),
58
59 /// Errors from file operations
60 #[error(transparent)]
61 File(#[from] crate::file::error::FileError),
62
63 /// Errors from chunk store operations
64 #[error(transparent)]
65 Store(#[from] crate::store::ChunkStoreError),
66
67 /// Errors from encryption operations
68 #[error(transparent)]
69 Encryption(#[from] crate::chunk::encryption::EncryptionError),
70
71 /// Input/output errors
72 #[error("I/O error: {0}")]
73 Io(#[from] std::io::Error),
74
75 /// Array conversion errors
76 #[error("Array conversion error: {0}")]
77 ArrayConversion(#[from] std::array::TryFromSliceError),
78}