debug_format.rs

  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    #[serde(default)]
 45    pub process_id: Option<u32>,
 46}
 47
 48/// Represents the launch request information of the debug adapter
 49#[derive(Deserialize, Serialize, Default, PartialEq, Eq, JsonSchema, Clone, Debug)]
 50pub struct LaunchConfig {
 51    /// The program that you trying to debug
 52    pub program: String,
 53    /// The current working directory of your project
 54    pub cwd: Option<PathBuf>,
 55}
 56
 57/// Represents the type that will determine which request to call on the debug adapter
 58#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
 59#[serde(rename_all = "lowercase", untagged)]
 60pub enum DebugRequestType {
 61    /// Call the `launch` request on the debug adapter
 62    Launch(LaunchConfig),
 63    /// Call the `attach` request on the debug adapter
 64    Attach(AttachConfig),
 65}
 66
 67/// Represents a request for starting the debugger.
 68/// Contrary to `DebugRequestType`, `DebugRequestDisposition` is not Serializable.
 69#[derive(PartialEq, Eq, Clone, Debug)]
 70pub enum DebugRequestDisposition {
 71    /// Debug session configured by the user.
 72    UserConfigured(DebugRequestType),
 73    /// Debug session configured by the debug adapter
 74    ReverseRequest(StartDebuggingRequestArguments),
 75}
 76
 77impl DebugRequestDisposition {
 78    /// Get the current working directory from request if it's a launch request and exits
 79    pub fn cwd(&self) -> Option<PathBuf> {
 80        match self {
 81            Self::UserConfigured(DebugRequestType::Launch(launch_config)) => {
 82                launch_config.cwd.clone()
 83            }
 84            _ => None,
 85        }
 86    }
 87}
 88/// Represents the configuration for the debug adapter
 89#[derive(PartialEq, Eq, Clone, Debug)]
 90pub struct DebugAdapterConfig {
 91    /// Name of the debug task
 92    pub label: String,
 93    /// The type of adapter you want to use
 94    pub adapter: String,
 95    /// The type of request that should be called on the debug adapter
 96    pub request: DebugRequestDisposition,
 97    /// Additional initialization arguments to be sent on DAP initialization
 98    pub initialize_args: Option<serde_json::Value>,
 99    /// Optional TCP connection information
100    ///
101    /// If provided, this will be used to connect to the debug adapter instead of
102    /// spawning a new process. This is useful for connecting to a debug adapter
103    /// that is already running or is started by another process.
104    pub tcp_connection: Option<TCPHost>,
105    /// What Locator to use to configure the debug task
106    pub locator: Option<String>,
107    /// Args to pass to a debug adapter (only used in locator right now)
108    pub args: Vec<String>,
109    /// Whether to tell the debug adapter to stop on entry
110    pub stop_on_entry: Option<bool>,
111}
112
113impl From<DebugTaskDefinition> for DebugAdapterConfig {
114    fn from(def: DebugTaskDefinition) -> Self {
115        Self {
116            label: def.label,
117            adapter: def.adapter,
118            request: DebugRequestDisposition::UserConfigured(def.request),
119            initialize_args: def.initialize_args,
120            tcp_connection: def.tcp_connection,
121            locator: def.locator,
122            args: def.args,
123            stop_on_entry: def.stop_on_entry,
124        }
125    }
126}
127
128impl TryFrom<DebugAdapterConfig> for DebugTaskDefinition {
129    type Error = ();
130    fn try_from(def: DebugAdapterConfig) -> Result<Self, Self::Error> {
131        let request = match def.request {
132            DebugRequestDisposition::UserConfigured(debug_request_type) => debug_request_type,
133            DebugRequestDisposition::ReverseRequest(_) => return Err(()),
134        };
135
136        Ok(Self {
137            label: def.label,
138            adapter: def.adapter,
139            request,
140            initialize_args: def.initialize_args,
141            tcp_connection: def.tcp_connection,
142            locator: def.locator,
143            args: def.args,
144            stop_on_entry: def.stop_on_entry,
145        })
146    }
147}
148
149impl DebugTaskDefinition {
150    /// Translate from debug definition to a task template
151    pub fn to_zed_format(self) -> anyhow::Result<TaskTemplate> {
152        let (command, cwd, request) = match self.request {
153            DebugRequestType::Launch(launch_config) => (
154                launch_config.program,
155                launch_config
156                    .cwd
157                    .map(|cwd| cwd.to_string_lossy().to_string()),
158                crate::task_template::DebugArgsRequest::Launch,
159            ),
160            DebugRequestType::Attach(attach_config) => (
161                "".to_owned(),
162                None,
163                crate::task_template::DebugArgsRequest::Attach(attach_config),
164            ),
165        };
166
167        let task_type = TaskType::Debug(DebugArgs {
168            adapter: self.adapter,
169            request,
170            initialize_args: self.initialize_args,
171            locator: self.locator,
172            tcp_connection: self.tcp_connection,
173            stop_on_entry: self.stop_on_entry,
174        });
175
176        let label = self.label.clone();
177
178        Ok(TaskTemplate {
179            label,
180            command,
181            args: vec![],
182            task_type,
183            cwd,
184            ..Default::default()
185        })
186    }
187}
188/// Represents the type of the debugger adapter connection
189#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
190#[serde(rename_all = "lowercase", tag = "connection")]
191pub enum DebugConnectionType {
192    /// Connect to the debug adapter via TCP
193    TCP(TCPHost),
194    /// Connect to the debug adapter via STDIO
195    STDIO,
196}
197
198/// This struct represent a user created debug task
199#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
200#[serde(rename_all = "snake_case")]
201pub struct DebugTaskDefinition {
202    /// The adapter to run
203    pub adapter: String,
204    /// The type of request that should be called on the debug adapter
205    #[serde(flatten)]
206    pub request: DebugRequestType,
207    /// Name of the debug task
208    pub label: String,
209    /// Additional initialization arguments to be sent on DAP initialization
210    pub initialize_args: Option<serde_json::Value>,
211    /// Optional TCP connection information
212    ///
213    /// If provided, this will be used to connect to the debug adapter instead of
214    /// spawning a new process. This is useful for connecting to a debug adapter
215    /// that is already running or is started by another process.
216    pub tcp_connection: Option<TCPHost>,
217    /// Locator to use
218    /// -- cargo
219    pub locator: Option<String>,
220    /// Args to pass to a debug adapter (only used in locator right now)
221    #[serde(skip)]
222    pub args: Vec<String>,
223    /// Whether to tell the debug adapter to stop on entry
224    pub stop_on_entry: Option<bool>,
225}
226
227/// A group of Debug Tasks defined in a JSON file.
228#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
229#[serde(transparent)]
230pub struct DebugTaskFile(pub Vec<DebugTaskDefinition>);
231
232impl DebugTaskFile {
233    /// Generates JSON schema of Tasks JSON template format.
234    pub fn generate_json_schema() -> serde_json_lenient::Value {
235        let schema = SchemaSettings::draft07()
236            .with(|settings| settings.option_add_null_type = false)
237            .into_generator()
238            .into_root_schema_for::<Self>();
239
240        serde_json_lenient::to_value(schema).unwrap()
241    }
242}
243
244impl TryFrom<DebugTaskFile> for TaskTemplates {
245    type Error = anyhow::Error;
246
247    fn try_from(value: DebugTaskFile) -> Result<Self, Self::Error> {
248        let templates = value
249            .0
250            .into_iter()
251            .filter_map(|debug_definition| debug_definition.to_zed_format().log_err())
252            .collect();
253
254        Ok(Self(templates))
255    }
256}