use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum MatrixError {
Dimensions {
k: usize,
p: usize,
},
VandermondeUnsafe {
k: usize,
p: usize,
},
Singular,
}
impl fmt::Display for MatrixError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::Dimensions { k, p } => write!(
f,
"unusable matrix dimensions k={k}, p={p}: need k >= 1, p >= 1, k + p <= 255"
),
Self::VandermondeUnsafe { k, p } => write!(
f,
"k={k}, p={p} is outside the safe Vandermonde region; use Matrix::cauchy"
),
Self::Singular => f.write_str("recovery submatrix is singular"),
}
}
}
impl core::error::Error for MatrixError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CodeError {
ShardCount {
expected: usize,
got: usize,
},
ShardLength {
index: usize,
expected: usize,
got: usize,
},
ShardIndex {
index: usize,
k: usize,
},
}
impl fmt::Display for CodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::ShardCount { expected, got } => {
write!(f, "wrong shard count: expected {expected}, got {got}")
}
Self::ShardLength {
index,
expected,
got,
} => write!(
f,
"shard {index} has length {got}, but this stripe's shard length is {expected}"
),
Self::ShardIndex { index, k } => {
write!(f, "shard index {index} out of range for k={k} sources")
}
}
}
}
impl core::error::Error for CodeError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RecoverError {
TooManyMissing {
missing: usize,
p: usize,
},
Matrix(MatrixError),
Code(CodeError),
}
impl fmt::Display for RecoverError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::TooManyMissing { missing, p } => {
write!(
f,
"{missing} shards missing, but only {p} parity shards exist"
)
}
Self::Matrix(e) => write!(f, "recovery matrix error: {e}"),
Self::Code(e) => write!(f, "recovery shard error: {e}"),
}
}
}
impl core::error::Error for RecoverError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Matrix(e) => Some(e),
Self::Code(e) => Some(e),
Self::TooManyMissing { .. } => None,
}
}
}
impl From<MatrixError> for RecoverError {
fn from(e: MatrixError) -> Self {
Self::Matrix(e)
}
}
impl From<CodeError> for RecoverError {
fn from(e: CodeError) -> Self {
Self::Code(e)
}
}