Skip to main content

simd_rs63/
error.rs

1use crate::{BLOCK_ALIGNMENT, N};
2
3/// Errors returned by [`encode`] and [`recover`].
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6pub enum Error {
7    /// Block size is zero or not a multiple of [`BLOCK_ALIGNMENT`].
8    InvalidBlockSize(usize),
9
10    /// Not all blocks have the same length. `expected` is the length of the first block.
11    BlockSizeMismatch {
12        /// The length inferred from the first block.
13        expected: usize,
14        /// The mismatched length.
15        got: usize,
16    },
17
18    /// The same block index appeared more than once across the inputs.
19    DuplicateIndex(usize),
20
21    /// A block index is out of range; valid indices are `0..N`.
22    IndexOutOfRange(usize),
23}
24
25impl std::fmt::Display for Error {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            Error::InvalidBlockSize(size) => write!(
29                f,
30                "block size {size} is not a positive multiple of {BLOCK_ALIGNMENT}"
31            ),
32            Error::BlockSizeMismatch { expected, got } => write!(
33                f,
34                "block size mismatch: expected {expected} bytes, got {got} bytes"
35            ),
36            Error::DuplicateIndex(idx) => write!(f, "duplicate block index {idx}"),
37            Error::IndexOutOfRange(idx) => {
38                write!(f, "block index {idx} is out of range (must be less than {N})")
39            }
40        }
41    }
42}
43
44impl std::error::Error for Error {}