Skip to content

Commit a5ef870

Browse files
committed
Move handle_request to separate file
1 parent 46b38d5 commit a5ef870

File tree

2 files changed

+165
-159
lines changed

2 files changed

+165
-159
lines changed

src/debugger.rs

Lines changed: 5 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
use dap::events::{Event, StoppedEventBody};
2-
use dap::prelude::{Command, Request, ResponseBody};
3-
use dap::responses::{
4-
EvaluateResponse, ScopesResponse, SetBreakpointsResponse, StackTraceResponse, ThreadsResponse,
5-
VariablesResponse,
6-
};
7-
use dap::types::{Breakpoint, Capabilities, Source, StackFrame, StoppedEventReason, Thread};
8-
use tracing::trace;
1+
use dap::events::Event;
2+
use dap::prelude::ResponseBody;
93

104
use crate::connection::Connection;
115

6+
mod handler;
7+
128
pub struct CairoDebugger {
139
connection: Connection,
1410
}
@@ -28,7 +24,7 @@ impl CairoDebugger {
2824

2925
pub fn run(&mut self) -> anyhow::Result<()> {
3026
while let Ok(req) = self.connection.next_request() {
31-
match handle_request(&req) {
27+
match handler::handle_request(&req) {
3228
ServerResponse::Success(body) => self.connection.send_success(req, body)?,
3329
ServerResponse::Error(msg) => self.connection.send_error(req, &msg)?,
3430
ServerResponse::Event(event) => self.connection.send_event(event)?,
@@ -42,153 +38,3 @@ impl CairoDebugger {
4238
Ok(())
4339
}
4440
}
45-
46-
fn handle_request(request: &Request) -> ServerResponse {
47-
match &request.command {
48-
// We have not yet decided if we want to support these.
49-
Command::BreakpointLocations(_)
50-
| Command::Cancel(_)
51-
| Command::Completions(_)
52-
| Command::DataBreakpointInfo(_)
53-
| Command::Disassemble(_)
54-
| Command::Disconnect(_)
55-
| Command::Goto(_)
56-
| Command::ExceptionInfo(_)
57-
| Command::GotoTargets(_)
58-
| Command::LoadedSources
59-
| Command::Modules(_)
60-
| Command::ReadMemory(_)
61-
| Command::RestartFrame(_)
62-
| Command::SetDataBreakpoints(_)
63-
| Command::Restart(_)
64-
| Command::SetExceptionBreakpoints(_)
65-
| Command::TerminateThreads(_)
66-
| Command::Terminate(_)
67-
| Command::StepInTargets(_)
68-
| Command::SetVariable(_)
69-
| Command::SetInstructionBreakpoints(_)
70-
| Command::SetExpression(_)
71-
| Command::WriteMemory(_) => {
72-
// If we receive these with current capabilities, it is the client's fault.
73-
let msg = format!("Received an unsupported request: {request:?}");
74-
ServerResponse::Error(msg)
75-
}
76-
77-
// These may be supported after the MVP.
78-
// Nonetheless, if we receive these with current capabilities,
79-
// it is the client's fault.
80-
Command::ReverseContinue(_) => {
81-
ServerResponse::Error("Step back is not yet supported".into())
82-
}
83-
Command::StepBack(_) => ServerResponse::Error("Step back is not yet supported".into()),
84-
Command::SetFunctionBreakpoints(_) => {
85-
ServerResponse::Error("Set function breakpoints is not yet supported".into())
86-
}
87-
88-
// It makes no sense to send `attach` in the current architecture.
89-
Command::Attach(_) => ServerResponse::Error("Attach is not supported".into()),
90-
91-
// Supported requests.
92-
Command::Initialize(args) => {
93-
trace!("Initialized a client: {:?}", args.client_name);
94-
95-
ServerResponse::Success(ResponseBody::Initialize(Capabilities {
96-
supports_configuration_done_request: Some(true),
97-
..Default::default()
98-
}))
99-
}
100-
Command::ConfigurationDone => {
101-
trace!("Configuration done");
102-
ServerResponse::Success(ResponseBody::ConfigurationDone)
103-
}
104-
Command::Continue(_) => {
105-
todo!()
106-
}
107-
Command::Launch(_) => {
108-
// Start running the Cairo program here.
109-
ServerResponse::SuccessThenEvent(ResponseBody::Launch, Event::Initialized)
110-
}
111-
Command::Next(_) => {
112-
todo!()
113-
}
114-
Command::Pause(_) => ServerResponse::Event(Event::Stopped(StoppedEventBody {
115-
reason: StoppedEventReason::Pause,
116-
thread_id: Some(0),
117-
description: None,
118-
preserve_focus_hint: None,
119-
text: None,
120-
all_threads_stopped: Some(true),
121-
hit_breakpoint_ids: None,
122-
})),
123-
Command::SetBreakpoints(args) => {
124-
let mut response_bps = Vec::new();
125-
if let Some(requested_bps) = &args.breakpoints {
126-
for bp in requested_bps {
127-
// For now accept every breakpoint as valid
128-
response_bps.push(Breakpoint {
129-
verified: true,
130-
source: Some(args.source.clone()),
131-
line: Some(bp.line),
132-
..Default::default()
133-
});
134-
}
135-
}
136-
ServerResponse::Success(ResponseBody::SetBreakpoints(SetBreakpointsResponse {
137-
breakpoints: response_bps,
138-
}))
139-
}
140-
Command::Source(_) => {
141-
todo!()
142-
}
143-
Command::StackTrace(_) => {
144-
ServerResponse::Success(ResponseBody::StackTrace(StackTraceResponse {
145-
stack_frames: vec![StackFrame {
146-
id: 1,
147-
name: "test".to_string(),
148-
// Replace it with the actual source path.
149-
// Otherwise, the debugger will crush after returning this response.
150-
source: Some(Source { name: None, path: None, ..Default::default() }),
151-
line: 1,
152-
column: 1,
153-
..Default::default()
154-
}],
155-
total_frames: Some(1),
156-
}))
157-
}
158-
Command::StepIn(_) => {
159-
todo!()
160-
}
161-
Command::StepOut(_) => {
162-
todo!()
163-
}
164-
165-
Command::Evaluate(_) => {
166-
ServerResponse::Success(ResponseBody::Evaluate(EvaluateResponse {
167-
// Return whatever since we cannot opt out of supporting this request.
168-
result: "".to_string(),
169-
type_field: None,
170-
presentation_hint: None,
171-
variables_reference: 0,
172-
named_variables: None,
173-
indexed_variables: None,
174-
memory_reference: None,
175-
}))
176-
}
177-
Command::Threads => {
178-
ServerResponse::Success(ResponseBody::Threads(ThreadsResponse {
179-
// Return a single thread.
180-
threads: vec![Thread { id: 0, name: "".to_string() }],
181-
}))
182-
}
183-
Command::Variables(_) => {
184-
ServerResponse::Success(ResponseBody::Variables(VariablesResponse {
185-
// Return no variables.
186-
variables: vec![],
187-
}))
188-
}
189-
Command::Scopes(_) => {
190-
// Return no scopes.
191-
ServerResponse::Success(ResponseBody::Scopes(ScopesResponse { scopes: vec![] }))
192-
}
193-
}
194-
}

src/debugger/handler.rs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
use dap::events::{Event, StoppedEventBody};
2+
use dap::prelude::{Command, Request, ResponseBody};
3+
use dap::responses::{
4+
EvaluateResponse, ScopesResponse, SetBreakpointsResponse, StackTraceResponse, ThreadsResponse,
5+
VariablesResponse,
6+
};
7+
use dap::types::{Breakpoint, Capabilities, Source, StackFrame, StoppedEventReason, Thread};
8+
use tracing::trace;
9+
10+
use crate::debugger::ServerResponse;
11+
12+
pub fn handle_request(request: &Request) -> ServerResponse {
13+
match &request.command {
14+
// We have not yet decided if we want to support these.
15+
Command::BreakpointLocations(_)
16+
| Command::Cancel(_)
17+
| Command::Completions(_)
18+
| Command::DataBreakpointInfo(_)
19+
| Command::Disassemble(_)
20+
| Command::Disconnect(_)
21+
| Command::Goto(_)
22+
| Command::ExceptionInfo(_)
23+
| Command::GotoTargets(_)
24+
| Command::LoadedSources
25+
| Command::Modules(_)
26+
| Command::ReadMemory(_)
27+
| Command::RestartFrame(_)
28+
| Command::SetDataBreakpoints(_)
29+
| Command::Restart(_)
30+
| Command::SetExceptionBreakpoints(_)
31+
| Command::TerminateThreads(_)
32+
| Command::Terminate(_)
33+
| Command::StepInTargets(_)
34+
| Command::SetVariable(_)
35+
| Command::SetInstructionBreakpoints(_)
36+
| Command::SetExpression(_)
37+
| Command::WriteMemory(_) => {
38+
// If we receive these with current capabilities, it is the client's fault.
39+
let msg = format!("Received an unsupported request: {request:?}");
40+
ServerResponse::Error(msg)
41+
}
42+
43+
// These may be supported after the MVP.
44+
// Nonetheless, if we receive these with current capabilities,
45+
// it is the client's fault.
46+
Command::ReverseContinue(_) => {
47+
ServerResponse::Error("Step back is not yet supported".into())
48+
}
49+
Command::StepBack(_) => ServerResponse::Error("Step back is not yet supported".into()),
50+
Command::SetFunctionBreakpoints(_) => {
51+
ServerResponse::Error("Set function breakpoints is not yet supported".into())
52+
}
53+
54+
// It makes no sense to send `attach` in the current architecture.
55+
Command::Attach(_) => ServerResponse::Error("Attach is not supported".into()),
56+
57+
// Supported requests.
58+
Command::Initialize(args) => {
59+
trace!("Initialized a client: {:?}", args.client_name);
60+
61+
ServerResponse::Success(ResponseBody::Initialize(Capabilities {
62+
supports_configuration_done_request: Some(true),
63+
..Default::default()
64+
}))
65+
}
66+
Command::ConfigurationDone => {
67+
trace!("Configuration done");
68+
ServerResponse::Success(ResponseBody::ConfigurationDone)
69+
}
70+
Command::Continue(_) => {
71+
todo!()
72+
}
73+
Command::Launch(_) => {
74+
// Start running the Cairo program here.
75+
ServerResponse::SuccessThenEvent(ResponseBody::Launch, Event::Initialized)
76+
}
77+
Command::Next(_) => {
78+
todo!()
79+
}
80+
Command::Pause(_) => ServerResponse::Event(Event::Stopped(StoppedEventBody {
81+
reason: StoppedEventReason::Pause,
82+
thread_id: Some(0),
83+
description: None,
84+
preserve_focus_hint: None,
85+
text: None,
86+
all_threads_stopped: Some(true),
87+
hit_breakpoint_ids: None,
88+
})),
89+
Command::SetBreakpoints(args) => {
90+
let mut response_bps = Vec::new();
91+
if let Some(requested_bps) = &args.breakpoints {
92+
for bp in requested_bps {
93+
// For now accept every breakpoint as valid
94+
response_bps.push(Breakpoint {
95+
verified: true,
96+
source: Some(args.source.clone()),
97+
line: Some(bp.line),
98+
..Default::default()
99+
});
100+
}
101+
}
102+
ServerResponse::Success(ResponseBody::SetBreakpoints(SetBreakpointsResponse {
103+
breakpoints: response_bps,
104+
}))
105+
}
106+
Command::Source(_) => {
107+
todo!()
108+
}
109+
Command::StackTrace(_) => {
110+
ServerResponse::Success(ResponseBody::StackTrace(StackTraceResponse {
111+
stack_frames: vec![StackFrame {
112+
id: 1,
113+
name: "test".to_string(),
114+
// Replace it with the actual source path.
115+
// Otherwise, the debugger will crush after returning this response.
116+
source: Some(Source { name: None, path: None, ..Default::default() }),
117+
line: 1,
118+
column: 1,
119+
..Default::default()
120+
}],
121+
total_frames: Some(1),
122+
}))
123+
}
124+
Command::StepIn(_) => {
125+
todo!()
126+
}
127+
Command::StepOut(_) => {
128+
todo!()
129+
}
130+
131+
Command::Evaluate(_) => {
132+
ServerResponse::Success(ResponseBody::Evaluate(EvaluateResponse {
133+
// Return whatever since we cannot opt out of supporting this request.
134+
result: "".to_string(),
135+
type_field: None,
136+
presentation_hint: None,
137+
variables_reference: 0,
138+
named_variables: None,
139+
indexed_variables: None,
140+
memory_reference: None,
141+
}))
142+
}
143+
Command::Threads => {
144+
ServerResponse::Success(ResponseBody::Threads(ThreadsResponse {
145+
// Return a single thread.
146+
threads: vec![Thread { id: 0, name: "".to_string() }],
147+
}))
148+
}
149+
Command::Variables(_) => {
150+
ServerResponse::Success(ResponseBody::Variables(VariablesResponse {
151+
// Return no variables.
152+
variables: vec![],
153+
}))
154+
}
155+
Command::Scopes(_) => {
156+
// Return no scopes.
157+
ServerResponse::Success(ResponseBody::Scopes(ScopesResponse { scopes: vec![] }))
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)