on master help sign in
dap: Split call/request in the same way LSP does
commit032aaffa153e534481385af3b9fd3b8217d2e253 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 3 Dec 2021, 12:41 (5 years ago) parent2dbf966
1 file changed+50−24
helix-dap/src/client.rs+50−24
helix-dap/src/client.rs+50−24
@@ -5,10 +5,13 @@ use crate::{
55};
66use helix_core::syntax::DebuggerQuirks;
77
8+use serde_json::Value;
9+
810use anyhow::anyhow;
911pub use log::{error, info};
1012use std::{
1113 collections::HashMap,
14+ future::Future,
1215 net::{IpAddr, Ipv4Addr, SocketAddr},
1316 path::PathBuf,
1417 process::Stdio,
@@ -205,27 +208,52 @@ impl Client {
205208 self.request_counter.fetch_add(1, Ordering::Relaxed)
206209 }
207210
208− async fn request<R: crate::types::Request>(
211+ /// Execute a RPC request on the debugger.
212+ fn call<R: crate::types::Request>(
209213 &self,
210214 arguments: R::Arguments,
211− ) -> Result<R::Result> {
212− let (callback_tx, mut callback_rx) = channel(1);
213−
214− let arguments = Some(serde_json::to_value(arguments)?);
215−
216− let req = Request {
217− back_ch: Some(callback_tx),
218− seq: self.next_request_id(),
219− command: R::COMMAND.to_string(),
220− arguments,
221− };
222−
223− self.server_tx
224− .send(req)
225− .expect("Failed to send request to debugger");
215+ ) -> impl Future<Output = Result<Value>>
216+ where
217+ R::Arguments: serde::Serialize,
218+ {
219+ let server_tx = self.server_tx.clone();
220+ let id = self.next_request_id();
221+
222+ async move {
223+ use std::time::Duration;
224+ use tokio::time::timeout;
225+
226+ let arguments = Some(serde_json::to_value(arguments)?);
227+
228+ let (callback_tx, mut callback_rx) = channel(1);
229+
230+ let req = Request {
231+ back_ch: Some(callback_tx),
232+ seq: id,
233+ command: R::COMMAND.to_string(),
234+ arguments,
235+ };
236+
237+ server_tx.send(req).map_err(|e| Error::Other(e.into()))?;
238+
239+ // TODO: specifiable timeout, delay other calls until initialize success
240+ timeout(Duration::from_secs(20), callback_rx.recv())
241+ .await
242+ .map_err(|_| Error::Timeout)? // return Timeout
243+ .ok_or(Error::StreamClosed)?
244+ .map(|response| response.body.unwrap_or_default())
245+ // TODO: check response.success
246+ }
247+ }
226248
227− let response = callback_rx.recv().await.unwrap()?;
228− let response = serde_json::from_value(response.body.unwrap_or_default())?;
249+ async fn request<R: crate::types::Request>(&self, params: R::Arguments) -> Result<R::Result>
250+ where
251+ R::Arguments: serde::Serialize,
252+ R::Result: core::fmt::Debug, // TODO: temporary
253+ {
254+ // a future that resolves into the response
255+ let json = self.call::<R>(params).await?;
256+ let response = serde_json::from_value(json)?;
229257 Ok(response)
230258 }
231259
@@ -260,18 +288,16 @@ impl Client {
260288 self.request::<requests::Disconnect>(()).await
261289 }
262290
263− pub async fn launch(&mut self, args: serde_json::Value) -> Result<()> {
291+ pub async fn launch(&mut self, args: serde_json::Value) -> Result<Value> {
264292 let response = self.request::<requests::Launch>(args).await?;
265293 log::error!("launch response {}", response);
266−
267− Ok(())
294+ Ok(response)
268295 }
269296
270− pub async fn attach(&mut self, args: serde_json::Value) -> Result<()> {
297+ pub async fn attach(&mut self, args: serde_json::Value) -> Result<Value> {
271298 let response = self.request::<requests::Attach>(args).await?;
272299 log::error!("attach response {}", response);
273−
274− Ok(())
300+ Ok(response)
275301 }
276302
277303 pub async fn set_breakpoints(