use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
NegativeRun { index: usize, run: i64 },
LengthMismatch {
what: &'static str,
left: usize,
right: usize,
},
InvalidEnds { index: usize },
InvalidInterval { index: usize, start: i64, end: i64 },
InvalidOffsets,
Overflow,
UnequalLength { left: i64, right: i64 },
LengthTooShort { requested: i64, required: i64 },
DuplicateKey { position: usize },
InKey { position: usize, source: Box<Error> },
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NegativeRun { index, run } => {
write!(f, "run {index} has negative length {run}")
}
Error::LengthMismatch { what, left, right } => {
write!(f, "{what} have different lengths ({left} and {right})")
}
Error::InvalidEnds { index } => write!(
f,
"run end {index} is not greater than the previous end (or 0)"
),
Error::InvalidInterval { index, start, end } => write!(
f,
"interval {index} [{start}, {end}) has a negative start or ends before it starts"
),
Error::InvalidOffsets => write!(
f,
"offsets must start at 0, never decrease and end at the number of runs"
),
Error::Overflow => write!(f, "total length does not fit in an i64"),
Error::UnequalLength { left, right } => write!(
f,
"lengths differ ({left} and {right}) and the shorter side has no fill value"
),
Error::LengthTooShort {
requested,
required,
} => write!(
f,
"requested length {requested} is shorter than the existing length {required}"
),
Error::DuplicateKey { position } => {
write!(f, "key at position {position} already exists in the batch")
}
Error::InKey { position, source } => {
write!(f, "key at output position {position}: {source}")
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::InKey { source, .. } => Some(source.as_ref()),
_ => None,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;