Skip to content

Commit

Permalink
Use Clippy tool optimized code (#171)
Browse files Browse the repository at this point in the history
* Use Clippy Fixed code

* Use Clippy Fixed code
  • Loading branch information
mrxiaozhuox authored Mar 11, 2022
1 parent 7044967 commit edd51d0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 76 deletions.
59 changes: 25 additions & 34 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,9 @@ async fn execute_http_function(
request.insert("queries", queries.into_py(py));
request.insert("headers", headers_python.into_py(py));

match data {
Some(res) => {
let data = res.into_py(py);
request.insert("body", data);
}
None => {}
if let Some(res) = data {
let data = res.into_py(py);
request.insert("body", data);
};

// this makes the request object to be accessible across every route
Expand All @@ -288,7 +285,7 @@ async fn execute_http_function(
if response_type == "static_file" {
let file_path = res.get("file_path").unwrap();
let contents = read_file(file_path);
res.insert("body".to_owned(), contents.to_owned());
res.insert("body".to_owned(), contents);
}
Ok(res)
})?;
Expand All @@ -302,12 +299,9 @@ async fn execute_http_function(
let handler = handler.as_ref(py);
request.insert("params", route_params.into_py(py));
request.insert("headers", headers_python.into_py(py));
match data {
Some(res) => {
let data = res.into_py(py);
request.insert("body", data);
}
None => {}
if let Some(res) = data {
let data = res.into_py(py);
request.insert("body", data);
};

let output: PyResult<&PyAny> = match number_of_params {
Expand All @@ -331,25 +325,22 @@ pub async fn execute_event_handler(
event_handler: Option<Arc<PyFunction>>,
event_loop: Arc<Py<PyAny>>,
) {
match event_handler {
Some(handler) => match &(*handler) {
PyFunction::SyncFunction(function) => {
println!("Startup event handler");
Python::with_gil(|py| {
function.call0(py).unwrap();
});
}
PyFunction::CoRoutine(function) => {
let future = Python::with_gil(|py| {
println!("Startup event handler async");

let coroutine = function.as_ref(py).call0().unwrap();
pyo3_asyncio::into_future_with_loop((*event_loop).as_ref(py), coroutine)
.unwrap()
});
future.await.unwrap();
}
},
None => {}
}
if let Some(handler) = event_handler { match &(*handler) {
PyFunction::SyncFunction(function) => {
println!("Startup event handler");
Python::with_gil(|py| {
function.call0(py).unwrap();
});
}
PyFunction::CoRoutine(function) => {
let future = Python::with_gil(|py| {
println!("Startup event handler async");

let coroutine = function.as_ref(py).call0().unwrap();
pyo3_asyncio::into_future_with_loop((*event_loop).as_ref(py), coroutine)
.unwrap()
});
future.await.unwrap();
}
} }
}
4 changes: 2 additions & 2 deletions src/routers/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ impl Router {
Err(_) => return None,
};

return self.get_relevant_map(method);
self.get_relevant_map(method)
} else {
return None;
None
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/routers/web_socket_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use pyo3::types::PyAny;

/// Contains the thread safe hashmaps of different routes
type WebSocketRoutes = RwLock<HashMap<String, HashMap<String, (PyFunction, u8)>>>;

pub struct WebSocketRouter {
web_socket_routes: RwLock<HashMap<String, HashMap<String, (PyFunction, u8)>>>,
web_socket_routes: WebSocketRoutes,
}

impl WebSocketRouter {
Expand All @@ -21,7 +23,7 @@ impl WebSocketRouter {
#[inline]
pub fn get_web_socket_map(
&self,
) -> &RwLock<HashMap<String, HashMap<String, (PyFunction, u8)>>> {
) -> &WebSocketRoutes {
&self.web_socket_routes
}

Expand Down
62 changes: 30 additions & 32 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,28 @@ async fn index(
// try reading about arc or rc
let mut queries = HashMap::new();

if req.query_string().len() > 0 {
let split = req.query_string().split("&");
if !req.query_string().is_empty() {
let split = req.query_string().split('&');
for s in split {
let params = s.split_once("=").unwrap_or((s, ""));
queries.insert(params.0.to_string(), params.1.to_string());
}
}

let _ = match middleware_router.get_route("BEFORE_REQUEST", req.uri().path()) {
Some(((handler_function, number_of_params), route_params)) => {
let x = handle_middleware_request(
handler_function,
number_of_params,
&headers,
&mut payload,
&req,
route_params,
queries.clone(),
)
.await;
println!("{:?}", x.to_string());
}
None => {}
let _ = if let Some(((handler_function, number_of_params), route_params)) =
middleware_router.get_route("BEFORE_REQUEST", req.uri().path())
{
let x = handle_middleware_request(
handler_function,
number_of_params,
&headers,
&mut payload,
&req,
route_params,
queries.clone(),
)
.await;
println!("{:?}", x.to_string());
};

let response = match router.get_route(req.method().clone(), req.uri().path()) {
Expand All @@ -356,21 +355,20 @@ async fn index(
}
};

let _ = match middleware_router.get_route("AFTER_REQUEST", req.uri().path()) {
Some(((handler_function, number_of_params), route_params)) => {
let x = handle_middleware_request(
handler_function,
number_of_params,
&headers,
&mut payload,
&req,
route_params,
queries.clone(),
)
.await;
println!("{:?}", x.to_string());
}
None => {}
let _ = if let Some(((handler_function, number_of_params), route_params)) =
middleware_router.get_route("AFTER_REQUEST", req.uri().path())
{
let x = handle_middleware_request(
handler_function,
number_of_params,
&headers,
&mut payload,
&req,
route_params,
queries.clone(),
)
.await;
println!("{:?}", x.to_string());
};

response
Expand Down
12 changes: 6 additions & 6 deletions src/web_socket_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ impl Actor for MyWs {
fn started(&mut self, ctx: &mut WebsocketContext<Self>) {
let handler_function = &self.router.get("connect").unwrap().0;
let _number_of_params = &self.router.get("connect").unwrap().1;
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self);
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self);

println!("Actor is alive");
}

fn stopped(&mut self, ctx: &mut WebsocketContext<Self>) {
let handler_function = &self.router.get("close").expect("No close function").0;
let _number_of_params = &self.router.get("close").unwrap().1;
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self);
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self);

println!("Actor is dead");
}
Expand All @@ -89,7 +89,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
let handler_function = &self.router.get("connect").unwrap().0;
let _number_of_params = &self.router.get("connect").unwrap().1;
println!("{:?}", handler_function);
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self);
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self);
ctx.pong(&msg)
}

Expand All @@ -102,15 +102,15 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
// need to also passs this text as a param
let handler_function = &self.router.get("message").unwrap().0;
let _number_of_params = &self.router.get("message").unwrap().1;
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self);
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self);
}

Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
Ok(ws::Message::Close(_close_reason)) => {
println!("Socket was closed");
let handler_function = &self.router.get("close").expect("No close function").0;
let _number_of_params = &self.router.get("close").unwrap().1;
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self);
execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self);
}
_ => (),
}
Expand All @@ -127,7 +127,7 @@ pub async fn start_web_socket(
let resp = ws::start(
MyWs {
router,
event_loop: event_loop.clone(),
event_loop,
},
&req,
stream,
Expand Down

0 comments on commit edd51d0

Please sign in to comment.