use std::fmt::Display;
use thiserror::Error;
use tokio::sync::{mpsc::error::SendError, oneshot::error::RecvError};
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum BatchError<E: Display> {
#[error("Unable to send item to the worker for batching: channel closed")]
Tx,
#[error("Error while waiting for batch results: channel closed. {}", .0)]
Rx(RecvError),
#[error("Batch item rejected: {0}")]
Rejected(RejectionReason),
#[error("The entire batch failed: {}", .0)]
BatchFailed(E),
}
#[derive(Debug)]
#[non_exhaustive]
pub enum RejectionReason {
BatchFull,
MaxConcurrency,
}
impl Display for RejectionReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
RejectionReason::BatchFull => "the batch is full",
RejectionReason::MaxConcurrency => "the key has reached maximum concurrency",
})
}
}
pub type Result<T, E> = std::result::Result<T, BatchError<E>>;
impl<E: Display> From<RecvError> for BatchError<E> {
fn from(rx_err: RecvError) -> Self {
BatchError::Rx(rx_err)
}
}
impl<T, E: Display> From<SendError<T>> for BatchError<E> {
fn from(_tx_err: SendError<T>) -> Self {
BatchError::Tx
}
}
impl<E> BatchError<E>
where
E: Display,
{
pub fn inner(self) -> Result<E, E> {
match self {
BatchError::BatchFailed(e) => Ok(e),
_ => Err(self),
}
}
}