@@ -1,11 +1,9 @@
1use crate::{
1use crate::{
2− transport::{Event, Payload, Request, Response, Transport},
2+ transport::{Event, Payload, Request, Transport},
6pub use log::{error, info};
6pub use log::{error, info};
8−use serde_json::{from_value, to_value, Value};
10 collections::HashMap,
8 collections::HashMap,
11 net::{IpAddr, Ipv4Addr},
9 net::{IpAddr, Ipv4Addr},
@@ -184,13 +182,18 @@ impl Client {
184 self.request_counter.fetch_add(1, Ordering::Relaxed)
182 self.request_counter.fetch_add(1, Ordering::Relaxed)
187− async fn request(&self, command: String, arguments: Option<Value>) -> Result<Response> {
185+ async fn request<R: crate::types::Request>(
187+ arguments: R::Arguments,
188+ ) -> Result<R::Result> {
188 let (callback_rx, mut callback_tx) = channel(1);
189 let (callback_rx, mut callback_tx) = channel(1);
191+ let arguments = Some(serde_json::to_value(arguments)?);
190 let req = Request {
193 let req = Request {
191 back_ch: Some(callback_rx),
194 back_ch: Some(callback_rx),
192 seq: self.next_request_id(),
195 seq: self.next_request_id(),
193− command,
196+ command: R::COMMAND.to_string(),
194 arguments,
197 arguments,
@@ -198,7 +201,9 @@ impl Client {
198 .send(req)
201 .send(req)
199 .expect("Failed to send request to debugger");
202 .expect("Failed to send request to debugger");
201− Ok(callback_tx.recv().await.unwrap()?)
204+ let response = callback_tx.recv().await.unwrap()?;
205+ let response = serde_json::from_value(response.body.unwrap_or_default())?;
204 pub fn capabilities(&self) -> &DebuggerCapabilities {
209 pub fn capabilities(&self) -> &DebuggerCapabilities {
@@ -224,33 +229,30 @@ impl Client {
224 supports_invalidated_event: Some(false),
229 supports_invalidated_event: Some(false),
227− let response = self
232+ let response = self.request::<requests::Initialize>(args).await?;
228− .request("initialize".to_owned(), to_value(args).ok())
230− self.capabilities = from_value(response.body.unwrap()).ok();
233+ self.capabilities = Some(response);
235 pub async fn disconnect(&mut self) -> Result<()> {
238 pub async fn disconnect(&mut self) -> Result<()> {
236− self.request("disconnect".to_owned(), None).await?;
239+ self.request::<requests::Disconnect>(()).await
240− pub async fn launch(&mut self, args: impl Serialize) -> Result<()> {
242+ pub async fn launch(&mut self, args: serde_json::Value) -> Result<()> {
241 let mut initialized = self.listen_for_event("initialized".to_owned()).await;
243 let mut initialized = self.listen_for_event("initialized".to_owned()).await;
243− let res = self.request("launch".to_owned(), to_value(args).ok());
245+ let res = self.request::<requests::Launch>(args);
244 let ev = initialized.recv();
246 let ev = initialized.recv();
245 join!(res, ev).0?;
247 join!(res, ev).0?;
250− pub async fn attach(&mut self, args: impl Serialize) -> Result<()> {
252+ pub async fn attach(&mut self, args: serde_json::Value) -> Result<()> {
251 let mut initialized = self.listen_for_event("initialized".to_owned()).await;
253 let mut initialized = self.listen_for_event("initialized".to_owned()).await;
253− let res = self.request("attach".to_owned(), to_value(args).ok());
255+ let res = self.request::<requests::Attach>(args);
254 let ev = initialized.recv();
256 let ev = initialized.recv();
255 join!(res, ev).0?;
257 join!(res, ev).0?;
@@ -277,29 +279,20 @@ impl Client {
277 source_modified: Some(false),
279 source_modified: Some(false),
280− let response = self
282+ let response = self.request::<requests::SetBreakpoints>(args).await?;
281− .request("setBreakpoints".to_owned(), to_value(args).ok())
283− let body: Option<requests::SetBreakpointsResponse> = from_value(response.body.unwrap()).ok();
285− Ok(body.map(|b| b.breakpoints).unwrap())
284+ Ok(response.breakpoints)
288 pub async fn configuration_done(&mut self) -> Result<()> {
287 pub async fn configuration_done(&mut self) -> Result<()> {
289− self.request("configurationDone".to_owned(), None).await?;
288+ self.request::<requests::ConfigurationDone>(()).await
293 pub async fn continue_thread(&mut self, thread_id: usize) -> Result<Option<bool>> {
291 pub async fn continue_thread(&mut self, thread_id: usize) -> Result<Option<bool>> {
294 let args = requests::ContinueArguments { thread_id };
292 let args = requests::ContinueArguments { thread_id };
296− let response = self
294+ let response = self.request::<requests::Continue>(args).await?;
297− .request("continue".to_owned(), to_value(args).ok())
300− let body: Option<requests::ContinueResponse> = from_value(response.body.unwrap()).ok();
302− Ok(body.map(|b| b.all_threads_continued).unwrap())
295+ Ok(response.all_threads_continued)
305 pub async fn stack_trace(
298 pub async fn stack_trace(
@@ -313,33 +306,20 @@ impl Client {
313 format: None,
306 format: None,
316− let response = self
309+ let response = self.request::<requests::StackTrace>(args).await?;
317− .request("stackTrace".to_owned(), to_value(args).ok())
320− let body: requests::StackTraceResponse = from_value(response.body.unwrap()).unwrap();
322− Ok((body.stack_frames, body.total_frames))
310+ Ok((response.stack_frames, response.total_frames))
325 pub async fn threads(&mut self) -> Result<Vec<Thread>> {
313 pub async fn threads(&mut self) -> Result<Vec<Thread>> {
326− let response = self.request("threads".to_owned(), None).await?;
314+ let response = self.request::<requests::Threads>(()).await?;
328− let body: requests::ThreadsResponse = from_value(response.body.unwrap()).unwrap();
330− Ok(body.threads)
315+ Ok(response.threads)
333 pub async fn scopes(&mut self, frame_id: usize) -> Result<Vec<Scope>> {
318 pub async fn scopes(&mut self, frame_id: usize) -> Result<Vec<Scope>> {
334 let args = requests::ScopesArguments { frame_id };
319 let args = requests::ScopesArguments { frame_id };
336− let response = self
321+ let response = self.request::<requests::Scopes>(args).await?;
337− .request("scopes".to_owned(), to_value(args).ok())
340− let body: requests::ScopesResponse = from_value(response.body.unwrap()).unwrap();
342− Ok(body.scopes)
322+ Ok(response.scopes)
345 pub async fn variables(&mut self, variables_reference: usize) -> Result<Vec<Variable>> {
325 pub async fn variables(&mut self, variables_reference: usize) -> Result<Vec<Variable>> {
@@ -351,12 +331,7 @@ impl Client {
351 format: None,
331 format: None,
354− let response = self
334+ let response = self.request::<requests::Variables>(args).await?;
355− .request("variables".to_owned(), to_value(args).ok())
358− let body: requests::VariablesResponse = from_value(response.body.unwrap()).unwrap();
360− Ok(body.variables)
335+ Ok(response.variables)