1use dap_types::StartDebuggingRequestArguments;
2use schemars::{JsonSchema, r#gen::SchemaSettings};
3use serde::{Deserialize, Serialize};
4use std::net::Ipv4Addr;
5use std::path::PathBuf;
6use util::ResultExt;
7
8use crate::{TaskTemplate, TaskTemplates, TaskType, task_template::DebugArgs};
9
10impl Default for DebugConnectionType {
11 fn default() -> Self {
12 DebugConnectionType::TCP(TCPHost::default())
13 }
14}
15
16/// Represents the host information of the debug adapter
17#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
18pub struct TCPHost {
19 /// The port that the debug adapter is listening on
20 ///
21 /// Default: We will try to find an open port
22 pub port: Option<u16>,
23 /// The host that the debug adapter is listening too
24 ///
25 /// Default: 127.0.0.1
26 pub host: Option<Ipv4Addr>,
27 /// The max amount of time in milliseconds to connect to a tcp DAP before returning an error
28 ///
29 /// Default: 2000ms
30 pub timeout: Option<u64>,
31}
32
33impl TCPHost {
34 /// Get the host or fallback to the default host
35 pub fn host(&self) -> Ipv4Addr {
36 self.host.unwrap_or_else(|| Ipv4Addr::new(127, 0, 0, 1))
37 }
38}
39
40/// Represents the attach request information of the debug adapter
41#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
42pub struct AttachConfig {
43 /// The processId to attach to, if left empty we will show a process picker
44 pub process_id: Option<u32>,
45}
46
47/// Represents the launch request information of the debug adapter
48#[derive(Deserialize, Serialize, Default, PartialEq, Eq, JsonSchema, Clone, Debug)]
49pub struct LaunchConfig {
50 /// The program that you trying to debug
51 pub program: String,
52 /// The current working directory of your project
53 pub cwd: Option<PathBuf>,
54 /// Arguments to pass to a debuggee
55 #[serde(default)]
56 pub args: Vec<String>,
57}
58
59/// Represents the type that will determine which request to call on the debug adapter
60#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
61#[serde(rename_all = "lowercase", untagged)]
62pub enum DebugRequestType {
63 /// Call the `launch` request on the debug adapter
64 Launch(LaunchConfig),
65 /// Call the `attach` request on the debug adapter
66 Attach(AttachConfig),
67}
68
69impl From<LaunchConfig> for DebugRequestType {
70 fn from(launch_config: LaunchConfig) -> Self {
71 DebugRequestType::Launch(launch_config)
72 }
73}
74
75impl From<AttachConfig> for DebugRequestType {
76 fn from(attach_config: AttachConfig) -> Self {
77 DebugRequestType::Attach(attach_config)
78 }
79}
80/// Represents a request for starting the debugger.
81/// Contrary to `DebugRequestType`, `DebugRequestDisposition` is not Serializable.
82#[derive(PartialEq, Eq, Clone, Debug)]
83pub enum DebugRequestDisposition {
84 /// Debug session configured by the user.
85 UserConfigured(DebugRequestType),
86 /// Debug session configured by the debug adapter
87 ReverseRequest(StartDebuggingRequestArguments),
88}
89
90impl DebugRequestDisposition {
91 /// Get the current working directory from request if it's a launch request and exits
92 pub fn cwd(&self) -> Option<PathBuf> {
93 match self {
94 Self::UserConfigured(DebugRequestType::Launch(launch_config)) => {
95 launch_config.cwd.clone()
96 }
97 _ => None,
98 }
99 }
100}
101/// Represents the configuration for the debug adapter
102#[derive(PartialEq, Eq, Clone, Debug)]
103pub struct DebugAdapterConfig {
104 /// Name of the debug task
105 pub label: String,
106 /// The type of adapter you want to use
107 pub adapter: String,
108 /// The type of request that should be called on the debug adapter
109 pub request: DebugRequestDisposition,
110 /// Additional initialization arguments to be sent on DAP initialization
111 pub initialize_args: Option<serde_json::Value>,
112 /// Optional TCP connection information
113 ///
114 /// If provided, this will be used to connect to the debug adapter instead of
115 /// spawning a new process. This is useful for connecting to a debug adapter
116 /// that is already running or is started by another process.
117 pub tcp_connection: Option<TCPHost>,
118 /// What Locator to use to configure the debug task
119 pub locator: Option<String>,
120 /// Whether to tell the debug adapter to stop on entry
121 pub stop_on_entry: Option<bool>,
122}
123
124impl From<DebugTaskDefinition> for DebugAdapterConfig {
125 fn from(def: DebugTaskDefinition) -> Self {
126 Self {
127 label: def.label,
128 adapter: def.adapter,
129 request: DebugRequestDisposition::UserConfigured(def.request),
130 initialize_args: def.initialize_args,
131 tcp_connection: def.tcp_connection,
132 locator: def.locator,
133 stop_on_entry: def.stop_on_entry,
134 }
135 }
136}
137
138impl TryFrom<DebugAdapterConfig> for DebugTaskDefinition {
139 type Error = ();
140 fn try_from(def: DebugAdapterConfig) -> Result<Self, Self::Error> {
141 let request = match def.request {
142 DebugRequestDisposition::UserConfigured(debug_request_type) => debug_request_type,
143 DebugRequestDisposition::ReverseRequest(_) => return Err(()),
144 };
145
146 Ok(Self {
147 label: def.label,
148 adapter: def.adapter,
149 request,
150 initialize_args: def.initialize_args,
151 tcp_connection: def.tcp_connection,
152 locator: def.locator,
153 stop_on_entry: def.stop_on_entry,
154 })
155 }
156}
157
158impl TryFrom<TaskTemplate> for DebugTaskDefinition {
159 type Error = ();
160
161 fn try_from(value: TaskTemplate) -> Result<Self, Self::Error> {
162 let TaskType::Debug(debug_args) = value.task_type else {
163 return Err(());
164 };
165
166 let request = match debug_args.request {
167 crate::DebugArgsRequest::Launch => DebugRequestType::Launch(LaunchConfig {
168 program: value.command,
169 cwd: value.cwd.map(PathBuf::from),
170 args: value.args,
171 }),
172 crate::DebugArgsRequest::Attach(attach_config) => {
173 DebugRequestType::Attach(attach_config)
174 }
175 };
176
177 Ok(DebugTaskDefinition {
178 adapter: debug_args.adapter,
179 request,
180 label: value.label,
181 initialize_args: debug_args.initialize_args,
182 tcp_connection: debug_args.tcp_connection,
183 locator: debug_args.locator,
184 stop_on_entry: debug_args.stop_on_entry,
185 })
186 }
187}
188
189impl DebugTaskDefinition {
190 /// Translate from debug definition to a task template
191 pub fn to_zed_format(self) -> anyhow::Result<TaskTemplate> {
192 let (command, cwd, request) = match self.request {
193 DebugRequestType::Launch(launch_config) => (
194 launch_config.program,
195 launch_config
196 .cwd
197 .map(|cwd| cwd.to_string_lossy().to_string()),
198 crate::task_template::DebugArgsRequest::Launch,
199 ),
200 DebugRequestType::Attach(attach_config) => (
201 "".to_owned(),
202 None,
203 crate::task_template::DebugArgsRequest::Attach(attach_config),
204 ),
205 };
206
207 let task_type = TaskType::Debug(DebugArgs {
208 adapter: self.adapter,
209 request,
210 initialize_args: self.initialize_args,
211 locator: self.locator,
212 tcp_connection: self.tcp_connection,
213 stop_on_entry: self.stop_on_entry,
214 });
215
216 let label = self.label.clone();
217
218 Ok(TaskTemplate {
219 label,
220 command,
221 args: vec![],
222 task_type,
223 cwd,
224 ..Default::default()
225 })
226 }
227}
228/// Represents the type of the debugger adapter connection
229#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
230#[serde(rename_all = "lowercase", tag = "connection")]
231pub enum DebugConnectionType {
232 /// Connect to the debug adapter via TCP
233 TCP(TCPHost),
234 /// Connect to the debug adapter via STDIO
235 STDIO,
236}
237
238/// This struct represent a user created debug task
239#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
240#[serde(rename_all = "snake_case")]
241pub struct DebugTaskDefinition {
242 /// The adapter to run
243 pub adapter: String,
244 /// The type of request that should be called on the debug adapter
245 #[serde(flatten)]
246 pub request: DebugRequestType,
247 /// Name of the debug task
248 pub label: String,
249 /// Additional initialization arguments to be sent on DAP initialization
250 pub initialize_args: Option<serde_json::Value>,
251 /// Optional TCP connection information
252 ///
253 /// If provided, this will be used to connect to the debug adapter instead of
254 /// spawning a new process. This is useful for connecting to a debug adapter
255 /// that is already running or is started by another process.
256 pub tcp_connection: Option<TCPHost>,
257 /// Locator to use
258 /// -- cargo
259 pub locator: Option<String>,
260 /// Whether to tell the debug adapter to stop on entry
261 pub stop_on_entry: Option<bool>,
262}
263
264/// A group of Debug Tasks defined in a JSON file.
265#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
266#[serde(transparent)]
267pub struct DebugTaskFile(pub Vec<DebugTaskDefinition>);
268
269impl DebugTaskFile {
270 /// Generates JSON schema of Tasks JSON template format.
271 pub fn generate_json_schema() -> serde_json_lenient::Value {
272 let schema = SchemaSettings::draft07()
273 .with(|settings| settings.option_add_null_type = false)
274 .into_generator()
275 .into_root_schema_for::<Self>();
276
277 serde_json_lenient::to_value(schema).unwrap()
278 }
279}
280
281impl TryFrom<DebugTaskFile> for TaskTemplates {
282 type Error = anyhow::Error;
283
284 fn try_from(value: DebugTaskFile) -> Result<Self, Self::Error> {
285 let templates = value
286 .0
287 .into_iter()
288 .filter_map(|debug_definition| debug_definition.to_zed_format().log_err())
289 .collect();
290
291 Ok(Self(templates))
292 }
293}
294
295#[cfg(test)]
296mod tests {
297 use crate::{DebugRequestType, LaunchConfig};
298
299 #[test]
300 fn test_can_deserialize_non_attach_task() {
301 let deserialized: DebugRequestType =
302 serde_json::from_str(r#"{"program": "cafebabe"}"#).unwrap();
303 assert_eq!(
304 deserialized,
305 DebugRequestType::Launch(LaunchConfig {
306 program: "cafebabe".to_owned(),
307 ..Default::default()
308 })
309 );
310 }
311}