Skip to content

Commit

Permalink
Merge pull request #179 from Totodore/ft-improve-adapter-build
Browse files Browse the repository at this point in the history
Improve adapter configuration
  • Loading branch information
Totodore authored Nov 26, 2023
2 parents 68a5424 + aa43b98 commit f6cadf2
Showing 1 changed file with 19 additions and 39 deletions.
58 changes: 19 additions & 39 deletions socketioxide/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ impl Default for SocketIoConfig {
/// A builder to create a [`SocketIo`] instance.
/// It contains everything to configure the socket.io server with a [`SocketIoConfig`].
/// It can be used to build either a Tower [`Layer`](tower::layer::Layer) or a [`Service`](tower::Service).
pub struct SocketIoBuilder {
pub struct SocketIoBuilder<A: Adapter = LocalAdapter> {
config: SocketIoConfig,
engine_config_builder: EngineIoConfigBuilder,
adapter: std::marker::PhantomData<A>,
}

impl SocketIoBuilder {
impl<A: Adapter> SocketIoBuilder<A> {
/// Creates a new [`SocketIoBuilder`] with default config
pub fn new() -> Self {
Self {
config: SocketIoConfig::default(),
engine_config_builder: EngineIoConfigBuilder::new().req_path("/socket.io".to_string()),
adapter: std::marker::PhantomData,
}
}

Expand Down Expand Up @@ -151,18 +153,19 @@ impl SocketIoBuilder {
self
}

/// Builds a [`SocketIoLayer`] and a [`SocketIo`] instance
///
/// The layer can be used as a tower layer
#[inline(always)]
pub fn build_layer(self) -> (SocketIoLayer, SocketIo) {
self.build_layer_with_adapter::<LocalAdapter>()
/// Sets a custom [`Adapter`] for this [`SocketIoBuilder`]
pub fn with_adapter<B: Adapter>(self) -> SocketIoBuilder<B> {
SocketIoBuilder {
config: self.config,
engine_config_builder: self.engine_config_builder,
adapter: std::marker::PhantomData,
}
}

/// Builds a [`SocketIoLayer`] and a [`SocketIo`] instance with a custom [`Adapter`]
/// Builds a [`SocketIoLayer`] and a [`SocketIo`] instance
///
/// The layer can be used as a tower layer
pub fn build_layer_with_adapter<A: Adapter>(mut self) -> (SocketIoLayer<A>, SocketIo<A>) {
pub fn build_layer(mut self) -> (SocketIoLayer<A>, SocketIo<A>) {
self.config.engine_config = self.engine_config_builder.build();

let (layer, client) = SocketIoLayer::from_config(Arc::new(self.config));
Expand All @@ -173,18 +176,7 @@ impl SocketIoBuilder {
///
/// This service will be a _standalone_ service that return a 404 error for every non-socket.io request
/// It can be used as a hyper service
#[inline(always)]
pub fn build_svc(self) -> (SocketIoService<NotFoundService>, SocketIo) {
self.build_svc_with_adapter::<LocalAdapter>()
}

/// Builds a [`SocketIoService`] and a [`SocketIo`] instance with a custom [`Adapter`]
///
/// This service will be a _standalone_ service that return a 404 error for every non-socket.io request
/// It can be used as a hyper service
pub fn build_svc_with_adapter<A: Adapter>(
mut self,
) -> (SocketIoService<NotFoundService, A>, SocketIo<A>) {
pub fn build_svc(mut self) -> (SocketIoService<NotFoundService>, SocketIo) {
self.config.engine_config = self.engine_config_builder.build();

let (svc, client) =
Expand All @@ -195,18 +187,7 @@ impl SocketIoBuilder {
/// Builds a [`SocketIoService`] and a [`SocketIo`] instance with an inner service
///
/// It can be used as a hyper service
#[inline(always)]
pub fn build_with_inner_svc<S: Clone>(self, svc: S) -> (SocketIoService<S>, SocketIo) {
self.build_with_inner_svc_with_adapter::<S, LocalAdapter>(svc)
}

/// Builds a [`SocketIoService`] and a [`SocketIo`] instance with an inner service and a custom [`Adapter`]
///
/// It can be used as a hyper service
pub fn build_with_inner_svc_with_adapter<S: Clone, A: Adapter>(
mut self,
svc: S,
) -> (SocketIoService<S, A>, SocketIo<A>) {
pub fn build_with_inner_svc<S: Clone>(mut self, svc: S) -> (SocketIoService<S>, SocketIo) {
self.config.engine_config = self.engine_config_builder.build();

let (svc, client) = SocketIoService::with_config_inner(svc, Arc::new(self.config));
Expand All @@ -227,36 +208,35 @@ pub struct SocketIo<A: Adapter = LocalAdapter>(Arc<Client<A>>);

impl SocketIo<LocalAdapter> {
/// Creates a new [`SocketIoBuilder`] with a default config
#[inline(always)]
pub fn builder() -> SocketIoBuilder {
SocketIoBuilder::new()
}

/// Creates a new [`SocketIoService`] and a [`SocketIo`] instance with a default config.
/// This service will be a _standalone_ service that return a 404 error for every non-socket.io request.
/// It can be used as a [`Service`](tower::Service) (see hyper example)
#[inline(always)]
pub fn new_svc() -> (SocketIoService<NotFoundService>, SocketIo) {
Self::builder().build_svc()
}

/// Creates a new [`SocketIoService`] and a [`SocketIo`] instance with a default config.
/// It can be used as a [`Service`](tower::Service) with an inner service (see warp example)
#[inline(always)]
pub fn new_inner_svc<S: Clone>(svc: S) -> (SocketIoService<S>, SocketIo) {
Self::builder().build_with_inner_svc(svc)
}

/// Builds a [`SocketIoLayer`] and a [`SocketIo`] instance with a default config.
/// It can be used as a tower [`Layer`](tower::layer::Layer) (see axum example)
#[inline(always)]
pub fn new_layer() -> (SocketIoLayer, SocketIo) {
Self::builder().build_layer()
}
}

impl<A: Adapter> SocketIo<A> {
/// Creates a new [`SocketIoBuilder`] with a default config and a specified [`Adapter`]
pub fn builder_with_adapter() -> SocketIoBuilder {
SocketIoBuilder::new()
}

/// Returns a reference to the [`SocketIoConfig`] used by this [`SocketIo`] instance
#[inline]
pub fn config(&self) -> &SocketIoConfig {
Expand Down

0 comments on commit f6cadf2

Please sign in to comment.