diff options
author | Dmitry Sharshakov | 2021-08-13 19:46:40 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-08-20 04:43:54 +0000 |
commit | 59d6b92e5b05a829dd2aeb0994afc51dec0da87f (patch) | |
tree | 4e5bd3249e0ea55d5b7374738187db5ccdb43fbb | |
parent | 9678df1c62cc3ff7f1efc3800f43f83069d2c6ef (diff) |
refactor response processing
-rw-r--r-- | helix-dap/src/transport.rs | 78 |
1 files changed, 37 insertions, 41 deletions
diff --git a/helix-dap/src/transport.rs b/helix-dap/src/transport.rs index 5f16df5f..37aa9e25 100644 --- a/helix-dap/src/transport.rs +++ b/helix-dap/src/transport.rs @@ -1,6 +1,6 @@ use crate::{Error, Result}; use anyhow::Context; -use log::{error, info}; +use log::{error, info, warn}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; @@ -162,54 +162,50 @@ impl Transport { Ok(()) } + fn process_response(res: Response) -> Result<Response> { + match res.success { + true => { + info!( + "<- DAP success ({}, in response to {})", + res.seq, res.request_seq + ); + + Ok(res) + } + false => { + error!( + "<- DAP error {:?} ({:?}) for command #{} {}", + res.message, res.body, res.request_seq, res.command + ); + + Err(Error::Other(anyhow::format_err!("{:?}", res.body))) + } + } + } + async fn process_server_message( &self, client_tx: &UnboundedSender<Payload>, msg: Payload, ) -> Result<()> { match msg { - Payload::Response(Response { - ref success, - ref seq, - request_seq, - ref command, - ref message, - ref body, - .. - }) => { - let result = match success { - true => { - info!("<- DAP success ({}, in response to {})", seq, request_seq); - if let Payload::Response(val) = msg { - Ok(val) - } else { - unreachable!(); - } + Payload::Response(res) => { + let request_seq = res.request_seq; + let tx = self.pending_requests.lock().await.remove(&request_seq); + + match tx { + Some(tx) => match tx.send(Self::process_response(res)).await { + Ok(_) => (), + Err(_) => error!( + "Tried sending response into a closed channel (id={:?}), original request likely timed out", + request_seq + ), } - false => { - error!( - "<- DAP error {:?} ({:?}) for command #{} {}", - message, body, request_seq, command - ); - - Err(Error::Other(anyhow::format_err!("{:?}", body))) + None => { + warn!("Response to nonexistent request #{}", res.request_seq); + client_tx.send(Payload::Response(res)).expect("Failed to send"); } - }; - - let tx = self - .pending_requests - .lock() - .await - .remove(&request_seq) - .expect("pending_request with id not found!"); - - match tx.send(result).await { - Ok(_) => (), - Err(_) => error!( - "Tried sending response into a closed channel (id={:?}), original request likely timed out", - request_seq - ), - }; + } Ok(()) } |