#[cfg(doc)]
use super::Port;
use super::{Alert, Direction};
pub type PacketHandler<'a> = Box<dyn FnMut(&[u8], Direction) + 'a>;
impl<'a, F> crate::convert::From<F> for PacketHandler<'a>
where
F: FnMut(&[u8], Direction) + 'a,
{
fn from(value: F) -> Self {
Box::new(value)
}
}
pub type SendPacketHandler<'a> = Box<dyn FnMut(&[u8], Direction) + Send + 'a>;
impl<'a, F> crate::convert::From<F> for SendPacketHandler<'a>
where
F: FnMut(&[u8], Direction) + Send + 'a,
{
fn from(value: F) -> Self {
Box::new(value)
}
}
pub type UnexpectedAlertHandler<'a> = Box<dyn FnMut(Alert) -> Result<(), Alert> + 'a>;
impl<'a, F> crate::convert::From<F> for UnexpectedAlertHandler<'a>
where
F: FnMut(Alert) -> Result<(), Alert> + 'a,
{
fn from(value: F) -> Self {
Box::new(value)
}
}
pub type SendUnexpectedAlertHandler<'a> = Box<dyn FnMut(Alert) -> Result<(), Alert> + Send + 'a>;
impl<'a, F> crate::convert::From<F> for SendUnexpectedAlertHandler<'a>
where
F: FnMut(Alert) -> Result<(), Alert> + Send + 'a,
{
fn from(value: F) -> Self {
Box::new(value)
}
}
#[derive(Default)]
pub struct LocalHandlers<'a> {
pub(super) packet: Option<PacketHandler<'a>>,
pub(super) unexpected_alert: Option<UnexpectedAlertHandler<'a>>,
}
impl std::fmt::Debug for LocalHandlers<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LocalHandlers").finish_non_exhaustive()
}
}
impl<'a> Handlers for LocalHandlers<'a> {
type PacketHandler = PacketHandler<'a>;
type UnexpectedAlertHandler = UnexpectedAlertHandler<'a>;
fn packet(&mut self) -> &mut Option<Self::PacketHandler> {
&mut self.packet
}
fn unexpected_alert(&mut self) -> &mut Option<Self::UnexpectedAlertHandler> {
&mut self.unexpected_alert
}
}
impl private::Sealed for LocalHandlers<'_> {}
#[derive(Default)]
pub struct SendHandlers<'a> {
pub(super) packets: Option<SendPacketHandler<'a>>,
pub(super) unexpected_alerts: Option<SendUnexpectedAlertHandler<'a>>,
}
impl std::fmt::Debug for SendHandlers<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SendHandlers").finish_non_exhaustive()
}
}
impl<'a> Handlers for SendHandlers<'a> {
type PacketHandler = SendPacketHandler<'a>;
type UnexpectedAlertHandler = SendUnexpectedAlertHandler<'a>;
fn packet(&mut self) -> &mut Option<Self::PacketHandler> {
&mut self.packets
}
fn unexpected_alert(&mut self) -> &mut Option<Self::UnexpectedAlertHandler> {
&mut self.unexpected_alerts
}
}
impl private::Sealed for SendHandlers<'_> {}
pub trait Handlers: Default + private::Sealed {
type PacketHandler;
type UnexpectedAlertHandler;
fn packet(&mut self) -> &mut Option<Self::PacketHandler>;
fn unexpected_alert(&mut self) -> &mut Option<Self::UnexpectedAlertHandler>;
}
mod private {
pub trait Sealed {}
}