Skip to content

Commit 0a954c9

Browse files
committed
Handle all requests inside handle_request
1 parent f54e6e0 commit 0a954c9

File tree

2 files changed

+108
-83
lines changed

2 files changed

+108
-83
lines changed

src/debugger.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use dap::events::Event;
2-
use dap::prelude::ResponseBody;
1+
use anyhow::Result;
32

43
use crate::connection::Connection;
54

@@ -9,30 +8,15 @@ pub struct CairoDebugger {
98
connection: Connection,
109
}
1110

12-
enum ServerResponse {
13-
Success(ResponseBody),
14-
Error(String),
15-
Event(Event),
16-
SuccessThenEvent(ResponseBody, Event),
17-
}
18-
1911
impl CairoDebugger {
20-
pub fn connect() -> anyhow::Result<Self> {
12+
pub fn connect() -> Result<Self> {
2113
let connection = Connection::new()?;
2214
Ok(Self { connection })
2315
}
2416

25-
pub fn run(&self) -> anyhow::Result<()> {
17+
pub fn run(&self) -> Result<()> {
2618
while let Ok(req) = self.connection.next_request() {
27-
match self.handle_request(&req) {
28-
ServerResponse::Success(body) => self.connection.send_success(req, body)?,
29-
ServerResponse::Error(msg) => self.connection.send_error(req, &msg)?,
30-
ServerResponse::Event(event) => self.connection.send_event(event)?,
31-
ServerResponse::SuccessThenEvent(body, event) => {
32-
self.connection.send_success(req, body)?;
33-
self.connection.send_event(event)?;
34-
}
35-
}
19+
self.handle_request(req)?;
3620
}
3721

3822
Ok(())

src/debugger/handler.rs

Lines changed: 104 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::{Result, bail};
12
use dap::events::{Event, StoppedEventBody};
23
use dap::prelude::{Command, Request, ResponseBody};
34
use dap::responses::{
@@ -6,20 +7,15 @@ use dap::responses::{
67
};
78
use dap::types::{Breakpoint, Capabilities, Source, StackFrame, StoppedEventReason, Thread};
89
use tracing::trace;
10+
911
use crate::CairoDebugger;
10-
use crate::debugger::ServerResponse;
1112

1213
pub enum HandleResult {
1314
Handled,
14-
Trigger(NextAction),
15-
}
16-
17-
pub enum NextAction {
18-
FinishInit,
1915
}
2016

2117
impl CairoDebugger {
22-
pub(crate) fn handle_request(&self, request: &Request) -> ServerResponse {
18+
pub(crate) fn handle_request(&self, request: Request) -> Result<HandleResult> {
2319
match &request.command {
2420
// We have not yet decided if we want to support these.
2521
Command::BreakpointLocations(_)
@@ -47,55 +43,75 @@ impl CairoDebugger {
4743
| Command::WriteMemory(_) => {
4844
// If we receive these with current capabilities, it is the client's fault.
4945
let msg = format!("Received an unsupported request: {request:?}");
50-
ServerResponse::Error(msg)
46+
self.connection.send_error(request, &msg)?;
47+
bail!("Unsupported request");
5148
}
5249

5350
// These may be supported after the MVP.
5451
// Nonetheless, if we receive these with current capabilities,
5552
// it is the client's fault.
5653
Command::ReverseContinue(_) => {
57-
ServerResponse::Error("Step back is not yet supported".into())
54+
self.connection.send_error(request, "Reverse continue is not yet supported")?;
55+
bail!("Reverse continue is not yet supported");
56+
}
57+
Command::StepBack(_) => {
58+
self.connection.send_error(request, "Step back is not yet supported")?;
59+
bail!("Step back is not yet supported");
5860
}
59-
Command::StepBack(_) => ServerResponse::Error("Step back is not yet supported".into()),
6061
Command::SetFunctionBreakpoints(_) => {
61-
ServerResponse::Error("Set function breakpoints is not yet supported".into())
62+
self.connection
63+
.send_error(request, "Set function breakpoints is not yet supported")?;
64+
bail!("Set function breakpoints is not yet supported");
6265
}
6366

6467
// It makes no sense to send `attach` in the current architecture.
65-
Command::Attach(_) => ServerResponse::Error("Attach is not supported".into()),
68+
Command::Attach(_) => {
69+
self.connection.send_error(request, "Attach is not supported")?;
70+
bail!("Unsupported request");
71+
}
6672

6773
// Supported requests.
6874
Command::Initialize(args) => {
6975
trace!("Initialized a client: {:?}", args.client_name);
70-
71-
ServerResponse::Success(ResponseBody::Initialize(Capabilities {
72-
supports_configuration_done_request: Some(true),
73-
..Default::default()
74-
}))
76+
self.connection.send_success(
77+
request,
78+
ResponseBody::Initialize(Capabilities {
79+
supports_configuration_done_request: Some(true),
80+
..Default::default()
81+
}),
82+
)?;
83+
self.connection.send_event(Event::Initialized)?;
84+
Ok(HandleResult::Handled)
7585
}
7686
Command::ConfigurationDone => {
7787
trace!("Configuration done");
78-
ServerResponse::Success(ResponseBody::ConfigurationDone)
88+
self.connection.send_success(request, ResponseBody::ConfigurationDone)?;
89+
Ok(HandleResult::Handled)
7990
}
8091
Command::Continue(_) => {
8192
todo!()
8293
}
8394
Command::Launch(_) => {
8495
// Start running the Cairo program here.
85-
ServerResponse::SuccessThenEvent(ResponseBody::Launch, Event::Initialized)
96+
self.connection.send_success(request, ResponseBody::Launch)?;
97+
Ok(HandleResult::Handled)
8698
}
8799
Command::Next(_) => {
88100
todo!()
89101
}
90-
Command::Pause(_) => ServerResponse::Event(Event::Stopped(StoppedEventBody {
91-
reason: StoppedEventReason::Pause,
92-
thread_id: Some(0),
93-
description: None,
94-
preserve_focus_hint: None,
95-
text: None,
96-
all_threads_stopped: Some(true),
97-
hit_breakpoint_ids: None,
98-
})),
102+
Command::Pause(_) => {
103+
self.connection.send_event(Event::Stopped(StoppedEventBody {
104+
reason: StoppedEventReason::Pause,
105+
thread_id: Some(0),
106+
description: None,
107+
preserve_focus_hint: None,
108+
text: None,
109+
all_threads_stopped: Some(true),
110+
hit_breakpoint_ids: None,
111+
}))?;
112+
self.connection.send_success(request, ResponseBody::Pause)?;
113+
Ok(HandleResult::Handled)
114+
}
99115
Command::SetBreakpoints(args) => {
100116
let mut response_bps = Vec::new();
101117
if let Some(requested_bps) = &args.breakpoints {
@@ -109,27 +125,35 @@ impl CairoDebugger {
109125
});
110126
}
111127
}
112-
ServerResponse::Success(ResponseBody::SetBreakpoints(SetBreakpointsResponse {
113-
breakpoints: response_bps,
114-
}))
128+
self.connection.send_success(
129+
request,
130+
ResponseBody::SetBreakpoints(SetBreakpointsResponse {
131+
breakpoints: response_bps,
132+
}),
133+
)?;
134+
Ok(HandleResult::Handled)
115135
}
116136
Command::Source(_) => {
117137
todo!()
118138
}
119139
Command::StackTrace(_) => {
120-
ServerResponse::Success(ResponseBody::StackTrace(StackTraceResponse {
121-
stack_frames: vec![StackFrame {
122-
id: 1,
123-
name: "test".to_string(),
124-
// Replace it with the actual source path.
125-
// Otherwise, the debugger will crush after returning this response.
126-
source: Some(Source { name: None, path: None, ..Default::default() }),
127-
line: 1,
128-
column: 1,
129-
..Default::default()
130-
}],
131-
total_frames: Some(1),
132-
}))
140+
self.connection.send_success(
141+
request,
142+
ResponseBody::StackTrace(StackTraceResponse {
143+
stack_frames: vec![StackFrame {
144+
id: 1,
145+
name: "test".to_string(),
146+
// Replace it with the actual source path.
147+
// Otherwise, the debugger will crush after returning this response.
148+
source: Some(Source { name: None, path: None, ..Default::default() }),
149+
line: 1,
150+
column: 1,
151+
..Default::default()
152+
}],
153+
total_frames: Some(1),
154+
}),
155+
)?;
156+
Ok(HandleResult::Handled)
133157
}
134158
Command::StepIn(_) => {
135159
todo!()
@@ -139,32 +163,49 @@ impl CairoDebugger {
139163
}
140164

141165
Command::Evaluate(_) => {
142-
ServerResponse::Success(ResponseBody::Evaluate(EvaluateResponse {
143-
// Return whatever since we cannot opt out of supporting this request.
144-
result: "".to_string(),
145-
type_field: None,
146-
presentation_hint: None,
147-
variables_reference: 0,
148-
named_variables: None,
149-
indexed_variables: None,
150-
memory_reference: None,
151-
}))
166+
self.connection.send_success(
167+
request,
168+
ResponseBody::Evaluate(EvaluateResponse {
169+
// Return whatever since we cannot opt out of supporting this request.
170+
result: "".to_string(),
171+
type_field: None,
172+
presentation_hint: None,
173+
variables_reference: 0,
174+
named_variables: None,
175+
indexed_variables: None,
176+
memory_reference: None,
177+
}),
178+
)?;
179+
Ok(HandleResult::Handled)
152180
}
153181
Command::Threads => {
154-
ServerResponse::Success(ResponseBody::Threads(ThreadsResponse {
155-
// Return a single thread.
156-
threads: vec![Thread { id: 0, name: "".to_string() }],
157-
}))
182+
self.connection.send_success(
183+
request,
184+
ResponseBody::Threads(ThreadsResponse {
185+
// Return a single thread.
186+
threads: vec![Thread { id: 0, name: "".to_string() }],
187+
}),
188+
)?;
189+
Ok(HandleResult::Handled)
158190
}
159191
Command::Variables(_) => {
160-
ServerResponse::Success(ResponseBody::Variables(VariablesResponse {
161-
// Return no variables.
162-
variables: vec![],
163-
}))
192+
self.connection.send_success(
193+
request,
194+
ResponseBody::Variables(VariablesResponse {
195+
// Return no variables.
196+
variables: vec![],
197+
}),
198+
)?;
199+
Ok(HandleResult::Handled)
164200
}
165201
Command::Scopes(_) => {
166202
// Return no scopes.
167-
ServerResponse::Success(ResponseBody::Scopes(ScopesResponse { scopes: vec![] }))
203+
// Return no scopes.
204+
self.connection.send_success(
205+
request,
206+
ResponseBody::Scopes(ScopesResponse { scopes: vec![] }),
207+
)?;
208+
Ok(HandleResult::Handled)
168209
}
169210
}
170211
}

0 commit comments

Comments
 (0)