From b154e5b2430ecf13dec03c3c8bd8c4af70f40c16 Mon Sep 17 00:00:00 2001 From: koe Date: Wed, 4 Oct 2023 12:50:05 -0500 Subject: [PATCH] formatting --- src/lib.rs | 6 ++--- src/server_runners/axum.rs | 6 ++--- src/server_runners/axum_tungstenite.rs | 2 +- src/server_runners/mod.rs | 1 - src/server_runners/tungstenite.rs | 35 ++++++++++++++------------ 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f82a648..2604f00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,10 @@ //! //! Refer to [`client`] or [`server`] module for detailed implementation guides. -mod socket; mod server_runners; +mod socket; + +pub use server_runners::*; pub use socket::CloseCode; pub use socket::CloseFrame; @@ -19,8 +21,6 @@ pub use socket::Socket; pub use socket::SocketConfig; pub use socket::Stream; -pub use server_runners::*; - cfg_if::cfg_if! { if #[cfg(feature = "client")] { pub mod client; diff --git a/src/server_runners/axum.rs b/src/server_runners/axum.rs index 97ef409..de9e82d 100644 --- a/src/server_runners/axum.rs +++ b/src/server_runners/axum.rs @@ -58,9 +58,9 @@ //! } //! ``` -use crate::socket::SocketConfig; -use crate::server_runners::axum_tungstenite::WebSocketUpgrade; use crate::server_runners::axum_tungstenite::rejection::*; +use crate::server_runners::axum_tungstenite::WebSocketUpgrade; +use crate::socket::SocketConfig; use crate::Server; use crate::ServerExt; use crate::Socket; @@ -117,7 +117,7 @@ where pure_req = pure_req.header(k, v); } let Ok(pure_req) = pure_req.body(()) else { - return Err(InvalidConnectionHeader{}.into()); + return Err(InvalidConnectionHeader {}.into()); }; Ok(Self { diff --git a/src/server_runners/axum_tungstenite.rs b/src/server_runners/axum_tungstenite.rs index f3072dc..dcf2cbb 100644 --- a/src/server_runners/axum_tungstenite.rs +++ b/src/server_runners/axum_tungstenite.rs @@ -212,7 +212,7 @@ impl WebSocketUpgrade { /// /// ``` /// use axum::response::Response; - /// use axum_tungstenite::WebSocketUpgrade; + /// use ezsockets::WebSocketUpgrade; /// /// async fn handler(ws: WebSocketUpgrade) -> Response { /// ws.on_failed_upgrade(|error| { diff --git a/src/server_runners/mod.rs b/src/server_runners/mod.rs index 5fdecc3..0a4c59a 100644 --- a/src/server_runners/mod.rs +++ b/src/server_runners/mod.rs @@ -1,4 +1,3 @@ - cfg_if::cfg_if! { if #[cfg(feature = "axum")] { pub mod axum; diff --git a/src/server_runners/tungstenite.rs b/src/server_runners/tungstenite.rs index 937ae31..4b1f2fa 100644 --- a/src/server_runners/tungstenite.rs +++ b/src/server_runners/tungstenite.rs @@ -31,19 +31,19 @@ //! } //! ``` -use crate::Server; +use crate::tungstenite::tungstenite::handshake::server::ErrorResponse; use crate::Error; +use crate::Request; +use crate::Server; +use crate::ServerExt; use crate::Socket; use crate::SocketConfig; -use crate::ServerExt; -use crate::Request; -use crate::tungstenite::tungstenite::handshake::server::ErrorResponse; use tokio_tungstenite::tungstenite; use tokio::net::TcpListener; -use tokio::net::ToSocketAddrs; use tokio::net::TcpStream; +use tokio::net::ToSocketAddrs; pub enum Acceptor { Plain, @@ -55,8 +55,10 @@ pub enum Acceptor { impl Acceptor { async fn accept(&self, stream: TcpStream) -> Result<(Socket, Request), Error> { - let mut req0 = None; - let callback = |req: &http::Request<()>, resp: http::Response<()>| -> Result, ErrorResponse> { + let mut req0 = None; + let callback = |req: &http::Request<()>, + resp: http::Response<()>| + -> Result, ErrorResponse> { let mut req1 = Request::builder() .method(req.method().clone()) .uri(req.uri().clone()) @@ -64,7 +66,9 @@ impl Acceptor { for (k, v) in req.headers() { req1 = req1.header(k, v); } - let Ok(body) = req1.body(()) else { return Err(ErrorResponse::default()); }; + let Ok(body) = req1.body(()) else { + return Err(ErrorResponse::default()); + }; req0 = Some(body); Ok(resp) @@ -87,7 +91,9 @@ impl Acceptor { Socket::new(socket, SocketConfig::default()) } }; - let Some(req_body) = req0 else { return Err("invalid request body".into()); }; + let Some(req_body) = req0 else { + return Err("invalid request body".into()); + }; Ok((socket, req_body)) } } @@ -98,7 +104,7 @@ async fn run_acceptor( acceptor: Acceptor, ) -> Result<(), Error> where - E: ServerExt + 'static + E: ServerExt + 'static, { loop { // TODO: Find a better way without those stupid matches @@ -107,7 +113,7 @@ where Err(err) => { tracing::warn!("failed to accept tcp connection: {:?}", err); continue; - }, + } }; let (socket, request) = match acceptor.accept(stream).await { Ok(socket) => socket, @@ -121,10 +127,7 @@ where } // Run the server -pub async fn run( - server: Server, - address: A, -) -> Result<(), Error> +pub async fn run(server: Server, address: A) -> Result<(), Error> where E: ServerExt + 'static, A: ToSocketAddrs, @@ -141,7 +144,7 @@ pub async fn run_on( acceptor: Acceptor, ) -> Result<(), Error> where - E: ServerExt + 'static + E: ServerExt + 'static, { run_acceptor(server, listener, acceptor).await }