| 1 |
mod client; |
| 2 |
pub mod registry; |
| 3 |
mod transport; |
| 4 |
|
| 5 |
pub use client::Client; |
| 6 |
pub use helix_dap_types::*; |
| 7 |
pub use transport::{Payload, Response, Transport}; |
| 8 |
|
| 9 |
use serde::de::DeserializeOwned; |
| 10 |
use std::collections::HashMap; |
| 11 |
|
| 12 |
use thiserror::Error; |
| 13 |
#[derive(Error, Debug)] |
| 14 |
pub enum Error { |
| 15 |
#[error("failed to parse: {0}")] |
| •16 |
Parse(#[from] serde_json::Error), |
| 17 |
#[error("IO Error: {0}")] |
| 18 |
IO(#[from] std::io::Error), |
| 19 |
#[error("request {0} timed out")] |
| 20 |
Timeout(u64), |
| 21 |
#[error("server closed the stream")] |
| 22 |
StreamClosed, |
| 23 |
#[error("Unhandled")] |
| 24 |
Unhandled, |
| 25 |
#[error(transparent)] |
| 26 |
ExecutableNotFound(#[from] helix_stdx::env::ExecutableNotFoundError), |
| 27 |
#[error(transparent)] |
| 28 |
Other(#[from] anyhow::Error), |
| 29 |
} |
| 30 |
pub type Result<T> = core::result::Result<T, Error>; |
| 31 |
|
| 32 |
#[derive(Debug)] |
| 33 |
pub enum Request { |
| 34 |
RunInTerminal(<requests::RunInTerminal as helix_dap_types::Request>::Arguments), |
| 35 |
StartDebugging(<requests::StartDebugging as helix_dap_types::Request>::Arguments), |
| 36 |
} |
| 37 |
|
| 38 |
impl Request { |
| 39 |
pub fn parse(command: &str, arguments: Option<serde_json::Value>) -> Result<Self> { |
| 40 |
use helix_dap_types::Request as _; |
| 41 |
|
| 42 |
let arguments = arguments.unwrap_or_default(); |
| 43 |
let request = match command { |
| 44 |
requests::RunInTerminal::COMMAND => Self::RunInTerminal(parse_value(arguments)?), |
| 45 |
requests::StartDebugging::COMMAND => Self::StartDebugging(parse_value(arguments)?), |
| 46 |
_ => return Err(Error::Unhandled), |
| 47 |
}; |
| 48 |
|
| 49 |
Ok(request) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug)] |
| 54 |
pub enum Event { |
| 55 |
Initialized(<events::Initialized as events::Event>::Body), |
| 56 |
Stopped(<events::Stopped as events::Event>::Body), |
| 57 |
Continued(<events::Continued as events::Event>::Body), |
| 58 |
Exited(<events::Exited as events::Event>::Body), |
| 59 |
Terminated(<events::Terminated as events::Event>::Body), |
| 60 |
Thread(<events::Thread as events::Event>::Body), |
| 61 |
Output(<events::Output as events::Event>::Body), |
| 62 |
Breakpoint(<events::Breakpoint as events::Event>::Body), |
| 63 |
Module(<events::Module as events::Event>::Body), |
| 64 |
LoadedSource(<events::LoadedSource as events::Event>::Body), |
| 65 |
Process(<events::Process as events::Event>::Body), |
| 66 |
Capabilities(<events::Capabilities as events::Event>::Body), |
| 67 |
ProgressStart(<events::ProgressStart as events::Event>::Body), |
| 68 |
ProgressUpdate(<events::ProgressUpdate as events::Event>::Body), |
| 69 |
ProgressEnd(<events::ProgressEnd as events::Event>::Body), |
| 70 |
|
| 71 |
Memory(<events::Memory as events::Event>::Body), |
| 72 |
} |
| 73 |
|
| 74 |
impl Event { |
| 75 |
pub fn parse(event: &str, body: Option<serde_json::Value>) -> Result<Self> { |
| 76 |
use crate::events::Event as _; |
| 77 |
|
| 78 |
let body = body.unwrap_or_default(); |
| 79 |
let event = match event { |
| 80 |
events::Initialized::EVENT => Self::Initialized(parse_value(body)?), |
| 81 |
events::Stopped::EVENT => Self::Stopped(parse_value(body)?), |
| 82 |
events::Continued::EVENT => Self::Continued(parse_value(body)?), |
| 83 |
events::Exited::EVENT => Self::Exited(parse_value(body)?), |
| 84 |
events::Terminated::EVENT => Self::Terminated(parse_value(body)?), |
| 85 |
events::Thread::EVENT => Self::Thread(parse_value(body)?), |
| 86 |
events::Output::EVENT => Self::Output(parse_value(body)?), |
| 87 |
events::Breakpoint::EVENT => Self::Breakpoint(parse_value(body)?), |
| 88 |
events::Module::EVENT => Self::Module(parse_value(body)?), |
| 89 |
events::LoadedSource::EVENT => Self::LoadedSource(parse_value(body)?), |
| 90 |
events::Process::EVENT => Self::Process(parse_value(body)?), |
| 91 |
events::Capabilities::EVENT => Self::Capabilities(parse_value(body)?), |
| 92 |
events::ProgressStart::EVENT => Self::ProgressStart(parse_value(body)?), |
| 93 |
events::ProgressUpdate::EVENT => Self::ProgressUpdate(parse_value(body)?), |
| 94 |
events::ProgressEnd::EVENT => Self::ProgressEnd(parse_value(body)?), |
| 95 |
events::Memory::EVENT => Self::Memory(parse_value(body)?), |
| 96 |
_ => return Err(Error::Unhandled), |
| 97 |
}; |
| 98 |
|
| 99 |
Ok(event) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
fn parse_value<T>(value: serde_json::Value) -> Result<T> |
| 104 |
where |
| 105 |
T: DeserializeOwned, |
| 106 |
{ |
| 107 |
serde_json::from_value(value).map_err(|err| err.into()) |
| 108 |
} |
| 109 |
|
| 110 |
#[derive(Debug, Clone)] |
| 111 |
pub struct ProgressState { |
| 112 |
title: String, |
| 113 |
message: Option<String>, |
| 114 |
percentage: Option<u8>, |
| 115 |
} |
| 116 |
|
| 117 |
impl ProgressState { |
| 118 |
pub fn new(title: String, message: Option<String>, percentage: Option<u8>) -> Self { |
| 119 |
Self { |
| 120 |
title, |
| 121 |
message, |
| 122 |
percentage, |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
pub fn update(&mut self, message: Option<String>, percentage: Option<u8>) { |
| 127 |
if let Some(message) = message { |
| 128 |
self.message = Some(message); |
| 129 |
} |
| 130 |
if let Some(percentage) = percentage { |
| 131 |
self.percentage = Some(percentage); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
pub fn status_line(&self) -> String { |
| 136 |
let mut status = format!("Debug: {}", self.title); |
| 137 |
if let Some(message) = self.message.as_deref() { |
| 138 |
status.push_str(" - "); |
| 139 |
status.push_str(message); |
| 140 |
} |
| 141 |
if let Some(percentage) = self.percentage { |
| 142 |
status.push_str(&format!(" ({}%)", percentage)); |
| 143 |
} |
| 144 |
status |
| 145 |
} |
| 146 |
|
| 147 |
pub fn end_status_line(&self, message: Option<&str>) -> String { |
| 148 |
let mut status = format!("Debug: {}", self.title); |
| 149 |
if let Some(message) = message.or(self.message.as_deref()) { |
| 150 |
status.push_str(" - "); |
| 151 |
status.push_str(message); |
| 152 |
} else { |
| 153 |
status.push_str(" finished"); |
| 154 |
} |
| 155 |
status |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
pub type ProgressMap = HashMap<String, ProgressState>; |