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