From 5c0d7ea99868387c9acf538894bc7ff6ff8f942a Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 29 Nov 2020 17:08:33 +0000 Subject: [PATCH] :recycle: Removing unwraps --- crates/ptth_relay/src/lib.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/crates/ptth_relay/src/lib.rs b/crates/ptth_relay/src/lib.rs index 8d04c84..73bb72a 100644 --- a/crates/ptth_relay/src/lib.rs +++ b/crates/ptth_relay/src/lib.rs @@ -152,18 +152,18 @@ impl RelayState { } fn ok_reply > (b: B) --> Response +-> Result , http::Error> { - Response::builder ().status (StatusCode::OK).body (b.into ()).unwrap () + Response::builder ().status (StatusCode::OK).body (b.into ()) } fn error_reply (status: StatusCode, b: &str) --> Response +-> Result , http::Error> { Response::builder () .status (status) .header ("content-type", "text/plain") - .body (format! ("{}\n", b).into ()).unwrap () + .body (format! ("{}\n", b).into ()) } // Servers will come here and either handle queued requests from parked clients, @@ -174,7 +174,7 @@ async fn handle_http_listen ( watcher_code: String, api_key: &[u8], ) --> Response +-> Result , http::Error> { let trip_error = error_reply (StatusCode::UNAUTHORIZED, "Bad X-ApiKey"); @@ -253,7 +253,7 @@ async fn handle_http_response ( state: Arc , req_id: String, ) - -> Response +-> Result , http::Error> { let (parts, mut body) = req.into_parts (); let resp_parts: http_serde::ResponseParts = rmp_serde::from_read_ref (&base64::decode (parts.headers.get (ptth_core::PTTH_MAGIC_HEADER).unwrap ()).unwrap ()).unwrap (); @@ -350,7 +350,7 @@ async fn handle_http_request ( state: Arc , watcher_code: String ) - -> Response +-> Result , http::Error> { { let config = state.config.read ().await; @@ -446,7 +446,6 @@ async fn handle_http_request ( debug! ("Unparked request {}", req_id); resp.body (body) - .unwrap () }, Ok (Err (ShuttingDownError::ShuttingDown)) => { error_reply (StatusCode::GATEWAY_TIMEOUT, "Relay shutting down") @@ -568,7 +567,7 @@ async fn handle_server_list_internal (state: &Arc ) async fn handle_server_list ( state: Arc -) -> Response +) -> Result , http::Error> { let page = handle_server_list_internal (&state).await; @@ -578,7 +577,7 @@ async fn handle_server_list ( #[instrument (level = "trace", skip (req, state))] async fn handle_all (req: Request , state: Arc ) --> Result , Infallible> +-> Result , http::Error> { let path = req.uri ().path (); //println! ("{}", path); @@ -591,18 +590,18 @@ async fn handle_all (req: Request , state: Arc ) // This is stuff the server can use. Clients can't // POST right now - return Ok (if let Some (request_code) = prefix_match ("/7ZSFUKGV/http_response/", path) { + return if let Some (request_code) = prefix_match ("/7ZSFUKGV/http_response/", path) { let request_code = request_code.into (); handle_http_response (req, state, request_code).await } else { error_reply (StatusCode::BAD_REQUEST, "Can't POST this") - }); + }; } - Ok (if let Some (listen_code) = prefix_match ("/7ZSFUKGV/http_listen/", path) { + if let Some (listen_code) = prefix_match ("/7ZSFUKGV/http_listen/", path) { let api_key = match api_key { - None => return Ok (error_reply (StatusCode::UNAUTHORIZED, "Can't register as server without an API key")), + None => return error_reply (StatusCode::UNAUTHORIZED, "Can't register as server without an API key"), Some (x) => x, }; handle_http_listen (state, listen_code.into (), api_key.as_bytes ()).await @@ -631,7 +630,7 @@ async fn handle_all (req: Request , state: Arc ) } else { error_reply (StatusCode::OK, "Hi") - }) + } } pub fn load_templates (asset_root: &Path)