use super::multi_packet::MultiPacketContext;
use crate::response::FrameStream;
pub(super) enum ActiveOutput<F, E> {
None,
Response(FrameStream<F, E>),
MultiPacket(MultiPacketContext<F>),
}
pub(super) struct ShutdownResult {
pub(super) correlation_id: Option<u64>,
pub(super) source_closed: bool,
pub(super) call_on_command_end: bool,
}
pub(super) struct MultiPacketCloseResult {
pub(super) correlation_id: Option<u64>,
}
#[expect(
clippy::struct_excessive_bools,
reason = "Availability flags are a natural fit for booleans; no state machine needed"
)]
#[derive(Clone, Copy)]
pub(super) struct EventAvailability {
pub(super) high: bool,
pub(super) low: bool,
pub(super) multi_packet: bool,
pub(super) response: bool,
}
impl<F, E> ActiveOutput<F, E> {
pub(super) fn is_response(&self) -> bool { matches!(self, Self::Response(_)) }
pub(super) fn is_multi_packet(&self) -> bool { matches!(self, Self::MultiPacket(_)) }
pub(super) fn multi_packet_mut(&mut self) -> Option<&mut MultiPacketContext<F>> {
match self {
Self::MultiPacket(ctx) => Some(ctx),
_ => Option::None,
}
}
pub(super) fn clear_response(&mut self) {
if matches!(self, Self::Response(_)) {
*self = Self::None;
}
}
pub(super) fn shutdown(&mut self) -> ShutdownResult {
match std::mem::replace(self, Self::None) {
Self::MultiPacket(mut ctx) => {
let correlation_id = ctx.correlation_id();
let source_closed = if let Some(rx) = ctx.channel_mut() {
rx.close();
true
} else {
false
};
ShutdownResult {
correlation_id,
source_closed,
call_on_command_end: true,
}
}
Self::Response(_) => ShutdownResult {
correlation_id: None,
source_closed: true,
call_on_command_end: false,
},
Self::None => ShutdownResult {
correlation_id: None,
source_closed: false,
call_on_command_end: false,
},
}
}
pub(super) fn close_multi_packet(&mut self) -> MultiPacketCloseResult {
let correlation_id = self.multi_packet_mut().and_then(|ctx| ctx.correlation_id());
if let Self::MultiPacket(ctx) = self
&& let Some(rx) = ctx.channel_mut()
{
rx.close();
}
MultiPacketCloseResult { correlation_id }
}
pub(super) fn clear(&mut self) { *self = Self::None; }
}