use std::error::Error;
use tokio::sync::mpsc;
use super::super::{
ConnectionActor,
ConnectionStateError,
output::AvailableOutput,
state::ActorState,
};
use crate::{
NoProtocolError,
response::{FrameStream, WireframeError},
};
#[derive(Clone, Copy, Debug)]
pub(crate) enum InstalledOutput {
Response,
MultiPacket,
}
impl InstalledOutput {
pub(crate) const fn available(self) -> AvailableOutput {
match self {
Self::Response => AvailableOutput::Response,
Self::MultiPacket => AvailableOutput::MultiPacket,
}
}
pub(crate) const fn suppression_message(self) -> &'static str {
match self {
Self::Response => "shutdown should suppress the installed response output",
Self::MultiPacket => "shutdown should suppress the installed multi-packet output",
}
}
pub(crate) fn actor_state(self) -> ActorState {
match self {
Self::Response => ActorState::new(true, false),
Self::MultiPacket => ActorState::new(false, true),
}
}
pub(crate) const fn is_retained(self, actor: &ConnectionActor<u8, NoProtocolError>) -> bool {
match self {
Self::Response => actor.active_output.is_response(),
Self::MultiPacket => actor.active_output.is_multi_packet(),
}
}
pub(crate) fn install_pending(
self,
actor: &mut ConnectionActor<u8, NoProtocolError>,
keep_alive: &mut Vec<mpsc::Sender<u8>>,
) -> Result<(), ConnectionStateError> {
match self {
Self::Response => {
let stream: FrameStream<u8, NoProtocolError> = Box::pin(futures::stream::pending());
actor.set_response(Some(stream))
}
Self::MultiPacket => {
let (tx, rx) = mpsc::channel::<u8>(1);
keep_alive.push(tx);
actor.set_multi_packet(Some(rx))
}
}
}
pub(crate) fn install_ready(
self,
actor: &mut ConnectionActor<u8, NoProtocolError>,
) -> Result<(), Box<dyn Error>> {
match self {
Self::Response => {
let frames = vec![Ok::<u8, WireframeError<NoProtocolError>>(7)];
actor.set_response(Some(Box::pin(futures::stream::iter(frames))))?;
}
Self::MultiPacket => {
let (tx, rx) = mpsc::channel::<u8>(1);
tx.try_send(7)?;
drop(tx);
actor.set_multi_packet(Some(rx))?;
}
}
Ok(())
}
}