use std::sync::Arc;
use moqtap_codec::version::DraftVersion;
macro_rules! dispatch_all {
(
$(
#[cfg(feature = $feat:literal)]
$variant:ident => $module:ident,
)+
) => {
pub enum AnyConnection {
$(
#[cfg(feature = $feat)]
#[doc = concat!("A draft-", $feat, " connection.")]
$variant(crate::$module::connection::Connection),
)+
}
impl AnyConnection {
#[allow(unreachable_code)]
pub fn draft(&self) -> DraftVersion {
match self {
$(
#[cfg(feature = $feat)]
Self::$variant(_) => DraftVersion::$variant,
)+
#[allow(unreachable_patterns)]
_ => unreachable!("AnyConnection has no enabled variants"),
}
}
#[allow(unused_variables)]
pub fn set_observer(&mut self, observer: Arc<dyn AnyConnectionObserver>) {
match self {
$(
#[cfg(feature = $feat)]
Self::$variant(c) => {
c.set_observer(Box::new($variant::Adapter(observer)));
}
)+
#[allow(unreachable_patterns)]
_ => {}
}
}
pub fn clear_observer(&mut self) {
match self {
$(
#[cfg(feature = $feat)]
Self::$variant(c) => c.clear_observer(),
)+
#[allow(unreachable_patterns)]
_ => {}
}
}
#[allow(unused_variables)]
pub fn close(&self, code: u32, reason: &[u8]) {
match self {
$(
#[cfg(feature = $feat)]
Self::$variant(c) => c.close(code, reason),
)+
#[allow(unreachable_patterns)]
_ => {}
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum AnyClientEvent {
$(
#[cfg(feature = $feat)]
#[doc = concat!("A draft-", $feat, " event.")]
$variant(crate::$module::event::ClientEvent),
)+
}
impl AnyClientEvent {
#[allow(unreachable_code)]
pub fn draft(&self) -> DraftVersion {
match self {
$(
#[cfg(feature = $feat)]
Self::$variant(_) => DraftVersion::$variant,
)+
#[allow(unreachable_patterns)]
_ => unreachable!("AnyClientEvent has no enabled variants"),
}
}
}
$(
#[cfg(feature = $feat)]
#[allow(non_snake_case)]
mod $variant {
use super::{AnyClientEvent, AnyConnectionObserver};
use std::sync::Arc;
pub(super) struct Adapter(pub(super) Arc<dyn AnyConnectionObserver>);
impl crate::$module::observer::ConnectionObserver for Adapter {
fn on_event(&self, event: &crate::$module::event::ClientEvent) {
self.0.on_event(&AnyClientEvent::$variant(event.clone()));
}
fn on_event_owned(&self, event: crate::$module::event::ClientEvent) {
self.0.on_event(&AnyClientEvent::$variant(event));
}
}
}
)+
};
}
dispatch_all! {
#[cfg(feature = "draft07")]
Draft07 => draft07,
#[cfg(feature = "draft08")]
Draft08 => draft08,
#[cfg(feature = "draft09")]
Draft09 => draft09,
#[cfg(feature = "draft10")]
Draft10 => draft10,
#[cfg(feature = "draft11")]
Draft11 => draft11,
#[cfg(feature = "draft12")]
Draft12 => draft12,
#[cfg(feature = "draft13")]
Draft13 => draft13,
#[cfg(feature = "draft14")]
Draft14 => draft14,
#[cfg(feature = "draft15")]
Draft15 => draft15,
#[cfg(feature = "draft16")]
Draft16 => draft16,
#[cfg(feature = "draft17")]
Draft17 => draft17,
}
pub trait AnyConnectionObserver: Send + Sync {
fn on_event(&self, event: &AnyClientEvent);
}
pub struct NoOpObserver;
impl AnyConnectionObserver for NoOpObserver {
fn on_event(&self, _event: &AnyClientEvent) {}
}