use std::io;
use tokio_util::sync::CancellationToken;
use super::{super::frames::InboundFrames, RecoveryCoordinator, RecoveryState};
use crate::app::{
frame_handling::Dispatch,
recovery_codec::{DecodeOrigin, DecodedItem},
};
pub(in crate::app::inbound_handler) async fn resolve_read_item<T>(
item: DecodedItem<T>,
inbound: &mut InboundFrames<DecodedItem<T>, T>,
recovery: &mut RecoveryCoordinator<'_>,
shutdown: &CancellationToken,
) -> io::Result<Option<T>> {
match item {
DecodedItem::Frame(frame) => {
recovery.record_decoded_frame();
Ok(Some(frame))
}
DecodedItem::Failure(failure) => recovery
.apply(
failure,
RecoveryState {
pipeline: &mut inbound.pipeline,
message_assembly: &mut inbound.message_assembly,
evicted_streams: &mut inbound.evicted_streams,
shutdown,
},
)
.await
.map_or(Ok(None), Err),
}
}
pub(in crate::app::inbound_handler) fn hold_or_end<T>(
item: DecodedItem<T>,
recovery: &RecoveryCoordinator<'_>,
) -> io::Result<DecodedItem<T>> {
match item {
DecodedItem::Failure(failure) if failure.origin() == DecodeOrigin::DecodeEof => {
Err(recovery.report_terminal(failure))
}
other => Ok(other),
}
}
pub(in crate::app::inbound_handler) fn report_pending_failure<T>(
inbound: &mut InboundFrames<DecodedItem<T>, T>,
recovery: &RecoveryCoordinator<'_>,
) {
if let Some(DecodedItem::Failure(failure)) = inbound.pending.take() {
recovery.report_terminal(failure);
}
}
pub(in crate::app::inbound_handler) fn settle_dispatch<T>(
dispatch: io::Result<Dispatch>,
inbound: &mut InboundFrames<DecodedItem<T>, T>,
recovery: &RecoveryCoordinator<'_>,
) -> io::Result<Dispatch> {
if matches!(dispatch, Ok(Dispatch::Continue)) {
return Ok(Dispatch::Continue);
}
report_pending_failure(inbound, recovery);
dispatch
}