@@ -1,8 +1,9 @@
11use crate::{
22 transport::{Payload, Request, Transport},
33 types::*,
4− Result,
4+ Error, Result,
55};
6+use anyhow::anyhow;
67pub use log::{error, info};
78use std::{
89 collections::HashMap,
@@ -35,6 +36,33 @@ pub struct Client {
3536}
3637
3738impl Client {
39+ // Spawn a process and communicate with it by either TCP or stdio
40+ pub async fn process(
41+ cfg: DebugAdapterConfig,
42+ id: usize,
43+ ) -> Result<(Self, UnboundedReceiver<Payload>)> {
44+ if cfg.transport == "tcp" && cfg.port_arg.is_some() {
45+ Self::tcp_process(
46+ &cfg.command,
47+ cfg.args.iter().map(|s| s.as_str()).collect(),
48+ &cfg.port_arg.unwrap(),
49+ id,
50+ )
51+ .await
52+ } else if cfg.transport == "stdio" {
53+ Self::stdio(
54+ &cfg.command,
55+ cfg.args.iter().map(|s| s.as_str()).collect(),
56+ id,
57+ )
58+ } else {
59+ Result::Err(Error::Other(anyhow!(
60+ "Incorrect transport {}",
61+ cfg.transport
62+ )))
63+ }
64+ }
65+
3866 pub fn streams(
3967 rx: Box<dyn AsyncBufRead + Unpin + Send>,
4068 tx: Box<dyn AsyncWrite + Unpin + Send>,
@@ -113,14 +141,16 @@ impl Client {
113141 }
114142
115143 pub async fn tcp_process(
116− config: DebugAdapterConfig,
144+ cmd: &str,
145+ args: Vec<&str>,
146+ port_format: &str,
117147 id: usize,
118148 ) -> Result<(Self, UnboundedReceiver<Payload>)> {
119149 let port = Self::get_port().await.unwrap();
120150
121− let process = Command::new(config.command)
122− .args(config.args)
123− .args(config.port_arg.replace("{}", &port.to_string()).split(' '))
151+ let process = Command::new(cmd)
152+ .args(args)
153+ .args(port_format.replace("{}", &port.to_string()).split(' '))
124154 // silence messages
125155 .stdin(Stdio::null())
126156 .stdout(Stdio::null())