use std::{fmt, num::NonZeroUsize};
use crate::{AdmissionFailure, Lane};
use super::ReactorId;
#[derive(Debug)]
pub enum ReactorGroupSendFailure {
Closed,
UnknownReactor {
reactor: ReactorId,
reactors: NonZeroUsize,
},
Mailbox(AdmissionFailure),
}
#[derive(Debug)]
pub struct ReactorGroupSendError<T> {
item: T,
lane: Lane,
failure: ReactorGroupSendFailure,
}
impl<T> ReactorGroupSendError<T> {
pub(super) const fn new(item: T, lane: Lane, failure: ReactorGroupSendFailure) -> Self {
Self {
item,
lane,
failure,
}
}
pub const fn lane(&self) -> Lane {
self.lane
}
pub const fn failure(&self) -> &ReactorGroupSendFailure {
&self.failure
}
pub fn into_item(self) -> T {
self.item
}
pub fn into_parts(self) -> (T, Lane, ReactorGroupSendFailure) {
(self.item, self.lane, self.failure)
}
}
impl fmt::Display for ReactorGroupSendFailure {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Closed => formatter.write_str("reactor group admission is closed"),
Self::UnknownReactor { reactor, reactors } => write!(
formatter,
"reactor {} is outside the static topology of {reactors}",
reactor.get()
),
Self::Mailbox(source) => {
write!(formatter, "reactor mailbox rejected ingress: {source}")
}
}
}
}
impl<T> fmt::Display for ReactorGroupSendError<T> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{} in {:?} lane", self.failure, self.lane)
}
}
impl<T: fmt::Debug> std::error::Error for ReactorGroupSendError<T> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.failure {
ReactorGroupSendFailure::Mailbox(source) => Some(source),
ReactorGroupSendFailure::Closed | ReactorGroupSendFailure::UnknownReactor { .. } => {
None
}
}
}
}