on master help sign in
refactor: use tagged enum for handling DAP payloads
279db98Dmitry Sharshakov
helix-dap/src/client.rs−1
@@ -461,7 +461,6 @@ impl Client {
461 let req = Request {
461 let req = Request {
462 back_ch: Some(callback_rx),
462 back_ch: Some(callback_rx),
463 seq: self.next_request_id(),
463 seq: self.next_request_id(),
464− msg_type: "request".to_owned(),
465 command,
464 command,
466 arguments,
465 arguments,
467 };
466 };
helix-dap/src/transport.rs+2−8
@@ -18,8 +18,6 @@ pub struct Request {
18 #[serde(skip)]
18 #[serde(skip)]
19 pub back_ch: Option<Sender<Result<Response>>>,
19 pub back_ch: Option<Sender<Result<Response>>>,
20 pub seq: u64,
20 pub seq: u64,
21− #[serde(rename = "type")]
22− pub msg_type: String,
23 pub command: String,
21 pub command: String,
24 pub arguments: Option<Value>,
22 pub arguments: Option<Value>,
25}
23}
@@ -27,8 +25,6 @@ pub struct Request {
27#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
25#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
28pub struct Response {
26pub struct Response {
29 // seq is omitted as unused and is not sent by some implementations
27 // seq is omitted as unused and is not sent by some implementations
30− #[serde(rename = "type")]
31− pub msg_type: String,
32 pub request_seq: u64,
28 pub request_seq: u64,
33 pub success: bool,
29 pub success: bool,
34 pub command: String,
30 pub command: String,
@@ -39,14 +35,12 @@ pub struct Response {
39#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
35#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
40pub struct Event {
36pub struct Event {
41 // seq is omitted as unused and is not sent by some implementations
37 // seq is omitted as unused and is not sent by some implementations
42− #[serde(rename = "type")]
43− pub msg_type: String,
44 pub event: String,
38 pub event: String,
45 pub body: Option<Value>,
39 pub body: Option<Value>,
46}
40}
47
41
48#[derive(Debug, Clone, Deserialize, Serialize)]
42#[derive(Debug, Clone, Deserialize, Serialize)]
49−#[serde(untagged)]
43+#[serde(tag = "type", rename_all = "camelCase")]
50pub enum Payload {
44pub enum Payload {
51 // type = "event"
45 // type = "event"
52 Event(Event),
46 Event(Event),
@@ -135,7 +129,7 @@ impl Transport {
135 server_stdin: &mut Box<dyn AsyncWrite + Unpin + Send>,
129 server_stdin: &mut Box<dyn AsyncWrite + Unpin + Send>,
136 req: Request,
130 req: Request,
137 ) -> Result<()> {
131 ) -> Result<()> {
138− let json = serde_json::to_string(&req)?;
132+ let json = serde_json::to_string(&Payload::Request(req.clone()))?;
139 if let Some(back) = req.back_ch {
133 if let Some(back) = req.back_ch {
140 self.pending_requests.lock().await.insert(req.seq, back);
134 self.pending_requests.lock().await.insert(req.seq, back);
141 }
135 }