use std::sync::Arc;
use super::{
ConnectionContext,
CustomProtocolHooks,
WireframeProtocol,
dispatch::ProtocolDispatch,
};
use crate::{codec::EofError, push::PushHandle};
pub enum ProtocolHooks<F, E> {
None,
Protocol(ProtocolDispatch<F, E>),
Custom(CustomProtocolHooks<F, E>),
}
impl<F, E> Default for ProtocolHooks<F, E> {
fn default() -> Self { Self::None }
}
impl<F, E> From<CustomProtocolHooks<F, E>> for ProtocolHooks<F, E> {
fn from(hooks: CustomProtocolHooks<F, E>) -> Self { Self::Custom(hooks) }
}
impl<F, E> ProtocolHooks<F, E> {
pub fn on_connection_setup(&mut self, handle: PushHandle<F>, ctx: &mut ConnectionContext) {
match self {
Self::None => {}
Self::Protocol(dispatch) => dispatch.on_connection_setup(handle, ctx),
Self::Custom(custom) => custom.on_connection_setup(handle, ctx),
}
}
pub fn before_send(&mut self, frame: &mut F, ctx: &mut ConnectionContext) {
match self {
Self::None => {}
Self::Protocol(dispatch) => dispatch.before_send(frame, ctx),
Self::Custom(custom) => custom.before_send(frame, ctx),
}
}
pub fn on_command_end(&mut self, ctx: &mut ConnectionContext) {
match self {
Self::None => {}
Self::Protocol(dispatch) => dispatch.on_command_end(ctx),
Self::Custom(custom) => custom.on_command_end(ctx),
}
}
pub fn handle_error(&mut self, error: E, ctx: &mut ConnectionContext) {
match self {
Self::None => {}
Self::Protocol(dispatch) => dispatch.handle_error(error, ctx),
Self::Custom(custom) => custom.handle_error(error, ctx),
}
}
pub fn stream_end_frame(&mut self, ctx: &mut ConnectionContext) -> Option<F> {
match self {
Self::None => None,
Self::Protocol(dispatch) => dispatch.stream_end_frame(ctx),
Self::Custom(custom) => custom.stream_end_frame(ctx),
}
}
pub fn on_eof(&mut self, error: &EofError, partial_data: &[u8], ctx: &mut ConnectionContext) {
match self {
Self::None => {}
Self::Protocol(dispatch) => dispatch.on_eof(error, partial_data, ctx),
Self::Custom(custom) => custom.on_eof(error, partial_data, ctx),
}
}
pub fn from_protocol<P>(protocol: &Arc<P>) -> Self
where
P: WireframeProtocol<Frame = F, ProtocolError = E> + ?Sized,
{
Self::Protocol(ProtocolDispatch::from_protocol(protocol))
}
}