Skip to main content

clay_codes/
error.rs

1//! Error types for Clay code operations
2
3/// Error type for Clay code operations
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum ClayError {
6    /// Invalid code parameters (k, m, d)
7    InvalidParameters(String),
8    /// Not enough helper nodes for repair
9    InsufficientHelpers { needed: usize, provided: usize },
10    /// Chunk size doesn't match expected sub-chunk alignment
11    InvalidChunkSize { expected: usize, actual: usize },
12    /// Helper provided insufficient data
13    InsufficientHelperData { helper: usize, expected: usize, actual: usize },
14    /// Chunks have inconsistent sizes
15    InconsistentChunkSizes { first_size: usize, mismatched_idx: usize, mismatched_size: usize },
16    /// Too many erasures to recover (max is m)
17    TooManyErasures { max: usize, actual: usize },
18    /// RS reconstruction failed
19    ReconstructionFailed(String),
20    /// Missing required y-section helper for repair
21    MissingYSectionHelper { lost_node: usize, missing_helper: usize },
22    /// Arithmetic overflow in parameter calculation
23    Overflow(String),
24}
25
26impl std::fmt::Display for ClayError {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            ClayError::InvalidParameters(msg) => write!(f, "Invalid parameters: {}", msg),
30            ClayError::InsufficientHelpers { needed, provided } => {
31                write!(f, "Insufficient helpers: need {}, got {}", needed, provided)
32            }
33            ClayError::InvalidChunkSize { expected, actual } => {
34                write!(f, "Invalid chunk size: expected divisible by {}, got {}", expected, actual)
35            }
36            ClayError::InsufficientHelperData { helper, expected, actual } => {
37                write!(f, "Helper {} provided {} bytes, expected {}", helper, actual, expected)
38            }
39            ClayError::InconsistentChunkSizes { first_size, mismatched_idx, mismatched_size } => {
40                write!(f, "Chunk {} has size {} but expected {} (same as first chunk)",
41                       mismatched_idx, mismatched_size, first_size)
42            }
43            ClayError::TooManyErasures { max, actual } => {
44                write!(f, "Too many erasures: max {} supported, got {}", max, actual)
45            }
46            ClayError::ReconstructionFailed(msg) => write!(f, "RS reconstruction failed: {}", msg),
47            ClayError::MissingYSectionHelper { lost_node, missing_helper } => {
48                write!(f, "Missing required y-section helper {} for repairing node {}",
49                       missing_helper, lost_node)
50            }
51            ClayError::Overflow(msg) => write!(f, "Arithmetic overflow: {}", msg),
52        }
53    }
54}
55
56impl std::error::Error for ClayError {}