debug_format.rs

  1use dap_types::StartDebuggingRequestArguments;
  2use schemars::{gen::SchemaSettings, JsonSchema};
  3use serde::{Deserialize, Serialize};
  4use std::net::Ipv4Addr;
  5use std::path::PathBuf;
  6use util::ResultExt;
  7
  8use crate::{task_template::DebugArgs, TaskTemplate, TaskTemplates, TaskType};
  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}
110
111impl From<DebugTaskDefinition> for DebugAdapterConfig {
112    fn from(def: DebugTaskDefinition) -> Self {
113        Self {
114            label: def.label,
115            adapter: def.adapter,
116            request: DebugRequestDisposition::UserConfigured(def.request),
117            initialize_args: def.initialize_args,
118            tcp_connection: def.tcp_connection,
119            locator: def.locator,
120            args: def.args,
121        }
122    }
123}
124
125impl TryFrom<DebugAdapterConfig> for DebugTaskDefinition {
126    type Error = ();
127    fn try_from(def: DebugAdapterConfig) -> Result<Self, Self::Error> {
128        let request = match def.request {
129            DebugRequestDisposition::UserConfigured(debug_request_type) => debug_request_type,
130            DebugRequestDisposition::ReverseRequest(_) => return Err(()),
131        };
132
133        Ok(Self {
134            label: def.label,
135            adapter: def.adapter,
136            request,
137            initialize_args: def.initialize_args,
138            tcp_connection: def.tcp_connection,
139            locator: def.locator,
140            args: def.args,
141        })
142    }
143}
144
145impl DebugTaskDefinition {
146    /// Translate from debug definition to a task template
147    pub fn to_zed_format(self) -> anyhow::Result<TaskTemplate> {
148        let (command, cwd, request) = match self.request {
149            DebugRequestType::Launch(launch_config) => (
150                launch_config.program,
151                launch_config
152                    .cwd
153                    .map(|cwd| cwd.to_string_lossy().to_string()),
154                crate::task_template::DebugArgsRequest::Launch,
155            ),
156            DebugRequestType::Attach(attach_config) => (
157                "".to_owned(),
158                None,
159                crate::task_template::DebugArgsRequest::Attach(attach_config),
160            ),
161        };
162
163        let task_type = TaskType::Debug(DebugArgs {
164            adapter: self.adapter,
165            request,
166            initialize_args: self.initialize_args,
167            locator: self.locator,
168            tcp_connection: self.tcp_connection,
169        });
170
171        let label = self.label.clone();
172
173        Ok(TaskTemplate {
174            label,
175            command,
176            args: vec![],
177            task_type,
178            cwd,
179            ..Default::default()
180        })
181    }
182}
183/// Represents the type of the debugger adapter connection
184#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
185#[serde(rename_all = "lowercase", tag = "connection")]
186pub enum DebugConnectionType {
187    /// Connect to the debug adapter via TCP
188    TCP(TCPHost),
189    /// Connect to the debug adapter via STDIO
190    STDIO,
191}
192
193/// This struct represent a user created debug task
194#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
195#[serde(rename_all = "snake_case")]
196pub struct DebugTaskDefinition {
197    /// The adapter to run
198    pub adapter: String,
199    /// The type of request that should be called on the debug adapter
200    #[serde(flatten)]
201    pub request: DebugRequestType,
202    /// Name of the debug task
203    pub label: String,
204    /// Additional initialization arguments to be sent on DAP initialization
205    pub initialize_args: Option<serde_json::Value>,
206    /// Optional TCP connection information
207    ///
208    /// If provided, this will be used to connect to the debug adapter instead of
209    /// spawning a new process. This is useful for connecting to a debug adapter
210    /// that is already running or is started by another process.
211    pub tcp_connection: Option<TCPHost>,
212    /// Locator to use
213    /// -- cargo
214    pub locator: Option<String>,
215    /// Args to pass to a debug adapter (only used in locator right now)
216    #[serde(skip)]
217    pub args: Vec<String>,
218}
219
220/// A group of Debug Tasks defined in a JSON file.
221#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
222#[serde(transparent)]
223pub struct DebugTaskFile(pub Vec<DebugTaskDefinition>);
224
225impl DebugTaskFile {
226    /// Generates JSON schema of Tasks JSON template format.
227    pub fn generate_json_schema() -> serde_json_lenient::Value {
228        let schema = SchemaSettings::draft07()
229            .with(|settings| settings.option_add_null_type = false)
230            .into_generator()
231            .into_root_schema_for::<Self>();
232
233        serde_json_lenient::to_value(schema).unwrap()
234    }
235}
236
237impl TryFrom<DebugTaskFile> for TaskTemplates {
238    type Error = anyhow::Error;
239
240    fn try_from(value: DebugTaskFile) -> Result<Self, Self::Error> {
241        let templates = value
242            .0
243            .into_iter()
244            .filter_map(|debug_definition| debug_definition.to_zed_format().log_err())
245            .collect();
246
247        Ok(Self(templates))
248    }
249}