Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB committed Oct 4, 2023
1 parent da32c0f commit b154e5b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/server_runners/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/server_runners/axum_tungstenite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<C> WebSocketUpgrade<C> {
///
/// ```
/// use axum::response::Response;
/// use axum_tungstenite::WebSocketUpgrade;
/// use ezsockets::WebSocketUpgrade;
///

Check failure on line 216 in src/server_runners/axum_tungstenite.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unresolved import `ezsockets::WebSocketUpgrade`
/// async fn handler(ws: WebSocketUpgrade) -> Response {
/// ws.on_failed_upgrade(|error| {
Expand Down
1 change: 0 additions & 1 deletion src/server_runners/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

cfg_if::cfg_if! {
if #[cfg(feature = "axum")] {
pub mod axum;
Expand Down
35 changes: 19 additions & 16 deletions src/server_runners/tungstenite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -55,16 +55,20 @@ 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<http::Response<()>, ErrorResponse> {
let mut req0 = None;
let callback = |req: &http::Request<()>,
resp: http::Response<()>|
-> Result<http::Response<()>, ErrorResponse> {
let mut req1 = Request::builder()
.method(req.method().clone())
.uri(req.uri().clone())
.version(req.version());
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)
Expand All @@ -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))
}
}
Expand All @@ -98,7 +104,7 @@ async fn run_acceptor<E>(
acceptor: Acceptor,
) -> Result<(), Error>
where
E: ServerExt + 'static
E: ServerExt + 'static,
{
loop {
// TODO: Find a better way without those stupid matches
Expand All @@ -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,
Expand All @@ -121,10 +127,7 @@ where
}

// Run the server
pub async fn run<E, A>(
server: Server<E>,
address: A,
) -> Result<(), Error>
pub async fn run<E, A>(server: Server<E>, address: A) -> Result<(), Error>
where
E: ServerExt + 'static,
A: ToSocketAddrs,
Expand All @@ -141,7 +144,7 @@ pub async fn run_on<E>(
acceptor: Acceptor,
) -> Result<(), Error>
where
E: ServerExt + 'static
E: ServerExt + 'static,
{
run_acceptor(server, listener, acceptor).await
}

0 comments on commit b154e5b

Please sign in to comment.