use std::{
error::Error,
fmt,
};
use super::BatchProcessResult;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChunkedBatchProcessError<E> {
CountShortfall {
expected: usize,
actual: usize,
result: BatchProcessResult,
},
CountExceeded {
expected: usize,
observed_at_least: usize,
result: BatchProcessResult,
},
ChunkFailed {
chunk_index: usize,
start_index: usize,
chunk_len: usize,
source: E,
result: BatchProcessResult,
},
}
impl<E> ChunkedBatchProcessError<E> {
#[inline]
pub const fn result(&self) -> &BatchProcessResult {
match self {
Self::CountShortfall { result, .. }
| Self::CountExceeded { result, .. }
| Self::ChunkFailed { result, .. } => result,
}
}
#[inline]
pub fn into_result(self) -> BatchProcessResult {
match self {
Self::CountShortfall { result, .. }
| Self::CountExceeded { result, .. }
| Self::ChunkFailed { result, .. } => result,
}
}
}
impl<E> fmt::Display for ChunkedBatchProcessError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CountShortfall {
expected, actual, ..
} => write!(
f,
"batch item count shortfall: expected {expected}, actual {actual}"
),
Self::CountExceeded {
expected,
observed_at_least,
..
} => write!(
f,
"batch item count exceeded: expected {expected}, observed at least {observed_at_least}"
),
Self::ChunkFailed {
chunk_index,
start_index,
chunk_len,
..
} => write!(
f,
"batch chunk {chunk_index} failed at item {start_index} with {chunk_len} items"
),
}
}
}
impl<E> Error for ChunkedBatchProcessError<E>
where
E: Error + 'static,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::ChunkFailed { source, .. } => Some(source),
Self::CountShortfall { .. } | Self::CountExceeded { .. } => None,
}
}
}