Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
mod client;
pub mod registry;
mod transport;

pub use client::Client;
pub use helix_dap_types::*;
pub use transport::{Payload, Response, Transport};

use serde::de::DeserializeOwned;
use std::collections::HashMap;

use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
    #[error("failed to parse: {0}")]
    Parse(Box<dyn std::error::Error + Send + Sync>),
    #[error("IO Error: {0}")]
    IO(#[from] std::io::Error),
    #[error("request {0} timed out")]
    Timeout(u64),
    #[error("server closed the stream")]
    StreamClosed,
    #[error("Unhandled")]
    Unhandled,
    #[error(transparent)]
    ExecutableNotFound(#[from] helix_stdx::env::ExecutableNotFoundError),
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}
pub type Result<T> = core::result::Result<T, Error>;

impl From<serde_json::Error> for Error {
    fn from(value: serde_json::Error) -> Self {
        Self::Parse(Box::new(value))
    }
}

impl From<sonic_rs::Error> for Error {
    fn from(value: sonic_rs::Error) -> Self {
        Self::Parse(Box::new(value))
    }
}

#[derive(Debug)]
pub enum Request {
    RunInTerminal(<requests::RunInTerminal as helix_dap_types::Request>::Arguments),
    StartDebugging(<requests::StartDebugging as helix_dap_types::Request>::Arguments),
}

impl Request {
    pub fn parse(command: &str, arguments: Option<serde_json::Value>) -> Result<Self> {
        use helix_dap_types::Request as _;

        let arguments = arguments.unwrap_or_default();
        let request = match command {
            requests::RunInTerminal::COMMAND => Self::RunInTerminal(parse_value(arguments)?),
            requests::StartDebugging::COMMAND => Self::StartDebugging(parse_value(arguments)?),
            _ => return Err(Error::Unhandled),
        };

        Ok(request)
    }
}

#[derive(Debug)]
pub enum Event {
    Initialized(<events::Initialized as events::Event>::Body),
    Stopped(<events::Stopped as events::Event>::Body),
    Continued(<events::Continued as events::Event>::Body),
    Exited(<events::Exited as events::Event>::Body),
    Terminated(<events::Terminated as events::Event>::Body),
    Thread(<events::Thread as events::Event>::Body),
    Output(<events::Output as events::Event>::Body),
    Breakpoint(<events::Breakpoint as events::Event>::Body),
    Module(<events::Module as events::Event>::Body),
    LoadedSource(<events::LoadedSource as events::Event>::Body),
    Process(<events::Process as events::Event>::Body),
    Capabilities(<events::Capabilities as events::Event>::Body),
    ProgressStart(<events::ProgressStart as events::Event>::Body),
    ProgressUpdate(<events::ProgressUpdate as events::Event>::Body),
    ProgressEnd(<events::ProgressEnd as events::Event>::Body),
    // Invalidated(),
    Memory(<events::Memory as events::Event>::Body),
}

impl Event {
    pub fn parse(event: &str, body: Option<serde_json::Value>) -> Result<Self> {
        use crate::events::Event as _;

        let body = body.unwrap_or_default();
        let event = match event {
            events::Initialized::EVENT => Self::Initialized(parse_value(body)?),
            events::Stopped::EVENT => Self::Stopped(parse_value(body)?),
            events::Continued::EVENT => Self::Continued(parse_value(body)?),
            events::Exited::EVENT => Self::Exited(parse_value(body)?),
            events::Terminated::EVENT => Self::Terminated(parse_value(body)?),
            events::Thread::EVENT => Self::Thread(parse_value(body)?),
            events::Output::EVENT => Self::Output(parse_value(body)?),
            events::Breakpoint::EVENT => Self::Breakpoint(parse_value(body)?),
            events::Module::EVENT => Self::Module(parse_value(body)?),
            events::LoadedSource::EVENT => Self::LoadedSource(parse_value(body)?),
            events::Process::EVENT => Self::Process(parse_value(body)?),
            events::Capabilities::EVENT => Self::Capabilities(parse_value(body)?),
            events::ProgressStart::EVENT => Self::ProgressStart(parse_value(body)?),
            events::ProgressUpdate::EVENT => Self::ProgressUpdate(parse_value(body)?),
            events::ProgressEnd::EVENT => Self::ProgressEnd(parse_value(body)?),
            events::Memory::EVENT => Self::Memory(parse_value(body)?),
            _ => return Err(Error::Unhandled),
        };

        Ok(event)
    }
}

fn parse_value<T>(value: serde_json::Value) -> Result<T>
where
    T: DeserializeOwned,
{
    serde_json::from_value(value).map_err(|err| err.into())
}

#[derive(Debug, Clone)]
pub struct ProgressState {
    title: String,
    message: Option<String>,
    percentage: Option<u8>,
}

impl ProgressState {
    pub fn new(title: String, message: Option<String>, percentage: Option<u8>) -> Self {
        Self {
            title,
            message,
            percentage,
        }
    }

    pub fn update(&mut self, message: Option<String>, percentage: Option<u8>) {
        if let Some(message) = message {
            self.message = Some(message);
        }
        if let Some(percentage) = percentage {
            self.percentage = Some(percentage);
        }
    }

    pub fn status_line(&self) -> String {
        let mut status = format!("Debug: {}", self.title);
        if let Some(message) = self.message.as_deref() {
            status.push_str(" - ");
            status.push_str(message);
        }
        if let Some(percentage) = self.percentage {
            status.push_str(&format!(" ({}%)", percentage));
        }
        status
    }

    pub fn end_status_line(&self, message: Option<&str>) -> String {
        let mut status = format!("Debug: {}", self.title);
        if let Some(message) = message.or(self.message.as_deref()) {
            status.push_str(" - ");
            status.push_str(message);
        } else {
            status.push_str(" finished");
        }
        status
    }
}

pub type ProgressMap = HashMap<String, ProgressState>;