on master help sign in
add rx dispatcher to examples
afeaba1Dmitry Sharshakov
helix-dap/examples/dap-dlv.rs+24−20
@@ -1,7 +1,7 @@
1−use helix_dap::{events, Client, Event, Result, SourceBreakpoint};
1+use helix_dap::{events, Client, Payload, Result, SourceBreakpoint};
2use serde::{Deserialize, Serialize};
2use serde::{Deserialize, Serialize};
3use serde_json::{from_value, to_value};
3use serde_json::{from_value, to_value};
4−use tokio::sync::mpsc::Receiver;
4+use tokio::sync::mpsc::UnboundedReceiver;
5
5
6#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
6#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
7#[serde(rename_all = "camelCase")]
7#[serde(rename_all = "camelCase")]
@@ -10,15 +10,26 @@ struct LaunchArguments {
10 program: String,
10 program: String,
11}
11}
12
12
13−async fn output(mut output_event: Receiver<Event>) {
13+async fn dispatch(mut rx: UnboundedReceiver<Payload>) {
14 loop {
14 loop {
15+ match rx.recv().await.unwrap() {
16+ Payload::Event(ev) => match &ev.event[..] {
17+ "output" => {
15− let body: events::Output =
18+ let body: events::Output = from_value(ev.body.unwrap()).unwrap();
16− from_value(output_event.recv().await.unwrap().body.unwrap()).unwrap();
17− println!(
19+ println!(
18− "> [{}] {}",
20+ "> [{}] {}",
19− body.category.unwrap_or("unknown".to_owned()),
21+ body.category.unwrap_or("unknown".to_owned()),
20− body.output
22+ body.output
21− );
23+ );
24+ }
25+ "stopped" => {
26+ println!("stopped");
27+ }
28+ _ => {}
29+ },
30+ Payload::Response(_) => unreachable!(),
31+ Payload::Request(_) => todo!(),
32+ };
22 }
33 }
23}
34}
24
35
@@ -35,12 +46,11 @@ pub async fn main() -> Result<()> {
35 .apply()
46 .apply()
36 .expect("Failed to set up logging");
47 .expect("Failed to set up logging");
37
48
49+ let (mut client, events) =
38− let client = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0).await;
50+ Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0).await?;
39 println!("create: {:?}", client);
51 println!("create: {:?}", client);
40− let mut client = client?;
41
52
42− let output_event = client.listen_for_event("output".to_owned()).await;
53+ tokio::spawn(dispatch(events));
43− tokio::spawn(output(output_event));
44
54
45 println!("init: {:?}", client.initialize("go".to_owned()).await);
55 println!("init: {:?}", client.initialize("go".to_owned()).await);
46 println!("caps: {:?}", client.capabilities());
56 println!("caps: {:?}", client.capabilities());
@@ -73,14 +83,8 @@ pub async fn main() -> Result<()> {
73 .read_line(&mut _in)
83 .read_line(&mut _in)
74 .expect("Failed to read line");
84 .expect("Failed to read line");
75
85
76− let mut stopped_event = client.listen_for_event("stopped".to_owned()).await;
77−
78 println!("configurationDone: {:?}", client.configuration_done().await);
86 println!("configurationDone: {:?}", client.configuration_done().await);
79
87
80− let stop: events::Stopped =
81− from_value(stopped_event.recv().await.unwrap().body.unwrap()).unwrap();
82− println!("stopped: {:?}", stop);
83−
84 let threads = client.threads().await?;
88 let threads = client.threads().await?;
85 println!("threads: {:#?}", threads);
89 println!("threads: {:#?}", threads);
86 let bt = client
90 let bt = client
helix-dap/examples/dap-lldb.rs+23−20
@@ -1,7 +1,7 @@
1−use helix_dap::{events, Client, Event, Result, SourceBreakpoint};
1+use helix_dap::{events, Client, Payload, Result, SourceBreakpoint};
2use serde::{Deserialize, Serialize};
2use serde::{Deserialize, Serialize};
3use serde_json::{from_value, to_value};
3use serde_json::{from_value, to_value};
4−use tokio::sync::mpsc::Receiver;
4+use tokio::sync::mpsc::UnboundedReceiver;
5
5
6#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
6#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
7#[serde(rename_all = "camelCase")]
7#[serde(rename_all = "camelCase")]
@@ -10,15 +10,26 @@ struct LaunchArguments {
10 console: String,
10 console: String,
11}
11}
12
12
13−async fn output(mut output_event: Receiver<Event>) {
13+async fn dispatch(mut rx: UnboundedReceiver<Payload>) {
14 loop {
14 loop {
15+ match rx.recv().await.unwrap() {
16+ Payload::Event(ev) => match &ev.event[..] {
17+ "output" => {
15− let body: events::Output =
18+ let body: events::Output = from_value(ev.body.unwrap()).unwrap();
16− from_value(output_event.recv().await.unwrap().body.unwrap()).unwrap();
17− println!(
19+ println!(
18− "> [{}] {}",
20+ "> [{}] {}",
19− body.category.unwrap_or("unknown".to_owned()),
21+ body.category.unwrap_or("unknown".to_owned()),
20− body.output
22+ body.output
21− );
23+ );
24+ }
25+ "stopped" => {
26+ println!("stopped");
27+ }
28+ _ => {}
29+ },
30+ Payload::Response(_) => unreachable!(),
31+ Payload::Request(_) => todo!(),
32+ };
22 }
33 }
23}
34}
24
35
@@ -35,12 +46,10 @@ pub async fn main() -> Result<()> {
35 .apply()
46 .apply()
36 .expect("Failed to set up logging");
47 .expect("Failed to set up logging");
37
48
38− let client = Client::tcp_process("lldb-vscode", vec![], "-p {}", 0).await;
49+ let (mut client, events) = Client::tcp_process("lldb-vscode", vec![], "-p {}", 0).await?;
39 println!("create: {:?}", client);
50 println!("create: {:?}", client);
40− let mut client = client?;
41
51
42− let output_event = client.listen_for_event("output".to_owned()).await;
52+ tokio::spawn(dispatch(events));
43− tokio::spawn(output(output_event));
44
53
45 println!("init: {:?}", client.initialize("lldb".to_owned()).await);
54 println!("init: {:?}", client.initialize("lldb".to_owned()).await);
46 println!("caps: {:?}", client.capabilities());
55 println!("caps: {:?}", client.capabilities());
@@ -73,14 +82,8 @@ pub async fn main() -> Result<()> {
73 .read_line(&mut _in)
82 .read_line(&mut _in)
74 .expect("Failed to read line");
83 .expect("Failed to read line");
75
84
76− let mut stopped_event = client.listen_for_event("stopped".to_owned()).await;
77−
78 println!("configurationDone: {:?}", client.configuration_done().await);
85 println!("configurationDone: {:?}", client.configuration_done().await);
79
86
80− let stop: events::Stopped =
81− from_value(stopped_event.recv().await.unwrap().body.unwrap()).unwrap();
82− println!("stopped: {:?}", stop);
83−
84 let threads = client.threads().await?;
87 let threads = client.threads().await?;
85 println!("threads: {:#?}", threads);
88 println!("threads: {:#?}", threads);
86 let bt = client
89 let bt = client