use std::fmt;
use tokio::sync::mpsc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MultiPacketStamp {
Disabled,
Enabled(Option<u64>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum MultiPacketTerminationReason {
Drained,
Disconnected,
Shutdown,
}
impl MultiPacketTerminationReason {
pub(super) const fn as_str(self) -> &'static str {
match self {
Self::Drained => "drained",
Self::Disconnected => "disconnected",
Self::Shutdown => "shutdown",
}
}
}
impl fmt::Display for MultiPacketTerminationReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.as_str()) }
}
pub(super) struct MultiPacketContext<F> {
channel: Option<mpsc::Receiver<F>>,
stamp: MultiPacketStamp,
}
impl<F> MultiPacketContext<F> {
pub(super) const fn new() -> Self {
Self {
channel: None,
stamp: MultiPacketStamp::Disabled,
}
}
pub(super) fn install(
&mut self,
channel: Option<mpsc::Receiver<F>>,
correlation_id: Option<u64>,
) {
let stamp = if channel.is_some() {
MultiPacketStamp::Enabled(correlation_id)
} else {
MultiPacketStamp::Disabled
};
self.channel = channel;
self.stamp = stamp;
}
pub(super) fn channel_mut(&mut self) -> Option<&mut mpsc::Receiver<F>> { self.channel.as_mut() }
pub(super) fn is_stamping_enabled(&self) -> bool {
matches!(self.stamp, MultiPacketStamp::Enabled(_))
}
pub(super) fn correlation_id(&self) -> Option<u64> {
match self.stamp {
MultiPacketStamp::Enabled(value) => value,
MultiPacketStamp::Disabled => None,
}
}
}