#[cfg(feature = "adapter-http-controllers")]
pub mod http;
#[cfg(feature = "adapter-socketio")]
pub mod socketio;
use parking_lot::{RawRwLock, RwLock, lock_api::RwLockReadGuard};
use std::{any::TypeId, collections::HashMap};
use sword_core::HasDeps;
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum AdapterKind {
#[cfg(feature = "adapter-http-controllers")]
HttpController,
#[cfg(feature = "adapter-socketio")]
SocketIo,
}
pub trait Adapter: HasDeps {
fn kind() -> AdapterKind;
fn type_id() -> TypeId;
}
pub struct AdapterRegistry {
adapters: RwLock<HashMap<AdapterKind, Vec<TypeId>>>,
}
impl AdapterRegistry {
pub(crate) fn new() -> Self {
Self {
adapters: RwLock::new(HashMap::new()),
}
}
pub fn register<A: Adapter>(&self) {
let mut adapter_registry_vec = self
.adapters
.read()
.get(&A::kind())
.cloned()
.unwrap_or_default();
adapter_registry_vec.push(A::type_id());
self.adapters
.write()
.insert(A::kind(), adapter_registry_vec);
}
pub(crate) fn read(
&self,
) -> RwLockReadGuard<'_, RawRwLock, HashMap<AdapterKind, Vec<TypeId>>> {
self.adapters.read()
}
}
impl Default for AdapterRegistry {
fn default() -> Self {
Self::new()
}
}