1use std::fmt::Display;
2
3use thiserror::Error;
4use tokio::sync::{mpsc::error::SendError, oneshot::error::RecvError};
5
6#[derive(Error, Debug)]
8#[non_exhaustive]
9pub enum BatchError<E: Display> {
10 #[error("Unable to send item to the worker for batching: channel closed")]
14 Tx,
15
16 #[error("Error while waiting for batch results: channel closed. {}", .0)]
20 Rx(RecvError),
21
22 #[error("Batch item rejected: {0}")]
26 Rejected(RejectionReason),
27
28 #[error("The entire batch failed: {}", .0)]
30 BatchFailed(E),
31}
32
33#[derive(Debug)]
34#[non_exhaustive]
35pub enum RejectionReason {
36 BatchFull,
38 MaxConcurrency,
40}
41
42impl Display for RejectionReason {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 f.write_str(match self {
45 RejectionReason::BatchFull => "the batch is full",
46 RejectionReason::MaxConcurrency => "the key has reached maximum concurrency",
47 })
48 }
49}
50
51pub type BatchResult<T, E> = std::result::Result<T, BatchError<E>>;
52
53impl<E: Display> From<RecvError> for BatchError<E> {
54 fn from(rx_err: RecvError) -> Self {
55 BatchError::Rx(rx_err)
56 }
57}
58
59impl<T, E: Display> From<SendError<T>> for BatchError<E> {
60 fn from(_tx_err: SendError<T>) -> Self {
61 BatchError::Tx
62 }
63}
64
65impl<E> BatchError<E>
66where
67 E: Display,
68{
69 pub fn inner(self) -> BatchResult<E, E> {
71 match self {
72 BatchError::BatchFailed(e) => Ok(e),
73 _ => Err(self),
74 }
75 }
76}