@@ -80,13 +80,36 @@ pub struct DapOutput {
8080}
8181
8282impl DapOutput {
83- pub fn response ( response : Response ) -> Self {
83+ pub fn new ( response : Response ) -> Self {
8484 Self {
8585 response,
8686 dap_events : vec ! [ ] ,
8787 console_events : vec ! [ ] ,
8888 }
8989 }
90+
91+ pub fn error ( req : Request , message : & str ) -> Self {
92+ Self :: new ( req. error ( message) )
93+ }
94+ }
95+
96+ /// The result of a handler method. Contains a `ResponseBody` (not a full
97+ /// `Response`) plus any events. The dispatcher wraps the body with
98+ /// `req.success()` or `req.error()` to produce a transport-ready `DapOutput`.
99+ pub ( crate ) struct HandlerOutput {
100+ pub body : ResponseBody ,
101+ pub dap_events : Vec < Event > ,
102+ pub console_events : Vec < DapConsoleEvent > ,
103+ }
104+
105+ impl HandlerOutput {
106+ fn new ( body : ResponseBody ) -> Self {
107+ Self {
108+ body,
109+ dap_events : vec ! [ ] ,
110+ console_events : vec ! [ ] ,
111+ }
112+ }
90113}
91114
92115/// Transport-agnostic handler for DAP requests. Translates each request
@@ -112,55 +135,50 @@ impl DapHandler {
112135 pub fn dispatch ( & self , req : Request ) -> DapOutput {
113136 let cmd = req. command . clone ( ) ;
114137
115- let err_req = req. clone ( ) ;
116138 let result = match cmd {
117- Command :: Initialize ( args) => self . handle_initialize ( req, args) ,
118- Command :: Attach ( args) => self . handle_attach ( req, args) ,
119- Command :: Disconnect ( args) => self . handle_disconnect ( req, args) ,
120- Command :: Restart ( args) => self . handle_restart ( req, args) ,
121- Command :: Threads => self . handle_threads ( req) ,
122- Command :: SetBreakpoints ( args) => self . handle_set_breakpoints ( req, args) ,
123- Command :: SetExceptionBreakpoints ( args) => {
124- self . handle_set_exception_breakpoints ( req, args)
125- } ,
126- Command :: StackTrace ( args) => self . handle_stacktrace ( req, args) ,
127- Command :: Source ( args) => self . handle_source ( req, args) ,
128- Command :: Scopes ( args) => self . handle_scopes ( req, args) ,
129- Command :: Variables ( args) => self . handle_variables ( req, args) ,
139+ Command :: Initialize ( args) => self . handle_initialize ( args) ,
140+ Command :: Attach ( args) => self . handle_attach ( args) ,
141+ Command :: Disconnect ( args) => self . handle_disconnect ( args) ,
142+ Command :: Restart ( args) => self . handle_restart ( args) ,
143+ Command :: Threads => self . handle_threads ( ) ,
144+ Command :: SetBreakpoints ( args) => self . handle_set_breakpoints ( args) ,
145+ Command :: SetExceptionBreakpoints ( args) => self . handle_set_exception_breakpoints ( args) ,
146+ Command :: StackTrace ( args) => self . handle_stacktrace ( args) ,
147+ Command :: Source ( args) => self . handle_source ( args) ,
148+ Command :: Scopes ( args) => self . handle_scopes ( args) ,
149+ Command :: Variables ( args) => self . handle_variables ( args) ,
130150 Command :: Continue ( args) => {
131151 let resp = ResponseBody :: Continue ( ContinueResponse {
132152 all_threads_continued : Some ( true ) ,
133153 } ) ;
134- self . handle_step ( req, args, DebugRequest :: Continue , resp)
135- } ,
136- Command :: Next ( args) => {
137- self . handle_step ( req, args, DebugRequest :: Next , ResponseBody :: Next )
154+ self . handle_step ( args, DebugRequest :: Continue , resp)
138155 } ,
156+ Command :: Next ( args) => self . handle_step ( args, DebugRequest :: Next , ResponseBody :: Next ) ,
139157 Command :: StepIn ( args) => {
140- self . handle_step ( req , args, DebugRequest :: StepIn , ResponseBody :: StepIn )
158+ self . handle_step ( args, DebugRequest :: StepIn , ResponseBody :: StepIn )
141159 } ,
142160 Command :: StepOut ( args) => {
143- self . handle_step ( req , args, DebugRequest :: StepOut , ResponseBody :: StepOut )
161+ self . handle_step ( args, DebugRequest :: StepOut , ResponseBody :: StepOut )
144162 } ,
145- Command :: Pause ( args) => self . handle_pause ( req , args) ,
163+ Command :: Pause ( args) => self . handle_pause ( args) ,
146164 _ => {
147165 log:: warn!( "DAP: Unknown request" ) ;
148- return DapOutput :: response ( err_req . error ( "Ark DAP: Unknown request" ) ) ;
166+ return DapOutput :: error ( req , "Ark DAP: Unknown request" ) ;
149167 } ,
150168 } ;
151169
152170 match result {
153- Ok ( output) => output,
154- Err ( err) => DapOutput :: response ( err_req. error ( & format ! ( "{err}" ) ) ) ,
171+ Ok ( output) => DapOutput {
172+ response : req. success ( output. body ) ,
173+ dap_events : output. dap_events ,
174+ console_events : output. console_events ,
175+ } ,
176+ Err ( err) => DapOutput :: error ( req, & format ! ( "{err}" ) ) ,
155177 }
156178 }
157179
158- fn handle_initialize (
159- & self ,
160- req : Request ,
161- _args : InitializeArguments ,
162- ) -> anyhow:: Result < DapOutput > {
163- let rsp = req. success ( ResponseBody :: Initialize ( types:: Capabilities {
180+ fn handle_initialize ( & self , _args : InitializeArguments ) -> anyhow:: Result < HandlerOutput > {
181+ let body = ResponseBody :: Initialize ( types:: Capabilities {
164182 supports_restart_request : Some ( true ) ,
165183 supports_exception_info_request : Some ( false ) ,
166184 exception_breakpoint_filters : Some ( vec ! [
@@ -194,9 +212,9 @@ impl DapHandler {
194212 supports_hit_conditional_breakpoints : Some ( true ) ,
195213 supports_log_points : Some ( true ) ,
196214 ..Default :: default ( )
197- } ) ) ;
198- Ok ( DapOutput {
199- response : rsp ,
215+ } ) ;
216+ Ok ( HandlerOutput {
217+ body ,
200218 dap_events : vec ! [ Event :: Initialized ] ,
201219 console_events : vec ! [ ] ,
202220 } )
@@ -219,9 +237,8 @@ impl DapHandler {
219237 // can restore their state when re-enabled without requiring re-sourcing.
220238 fn handle_set_breakpoints (
221239 & self ,
222- req : Request ,
223240 args : SetBreakpointsArguments ,
224- ) -> anyhow:: Result < DapOutput > {
241+ ) -> anyhow:: Result < HandlerOutput > {
225242 let Some ( path) = args. source . path . as_ref ( ) else {
226243 return Err ( anyhow:: anyhow!( "Missing a path to set breakpoints for" ) ) ;
227244 } ;
@@ -233,10 +250,11 @@ impl DapHandler {
233250 Ok ( uri) => uri,
234251 Err ( err) => {
235252 log:: warn!( "Can't set breakpoints for non-file path: '{path}': {err}" ) ;
236- let rsp = req. success ( ResponseBody :: SetBreakpoints ( SetBreakpointsResponse {
237- breakpoints : vec ! [ ] ,
238- } ) ) ;
239- return Ok ( DapOutput :: response ( rsp) ) ;
253+ return Ok ( HandlerOutput :: new ( ResponseBody :: SetBreakpoints (
254+ SetBreakpointsResponse {
255+ breakpoints : vec ! [ ] ,
256+ } ,
257+ ) ) ) ;
240258 } ,
241259 } ;
242260
@@ -262,10 +280,9 @@ impl DapHandler {
262280 } )
263281 . collect ( ) ;
264282
265- let rsp = req. success ( ResponseBody :: SetBreakpoints ( SetBreakpointsResponse {
266- breakpoints,
267- } ) ) ;
268- return Ok ( DapOutput :: response ( rsp) ) ;
283+ return Ok ( HandlerOutput :: new ( ResponseBody :: SetBreakpoints (
284+ SetBreakpointsResponse { breakpoints } ,
285+ ) ) ) ;
269286 } ,
270287 } ;
271288
@@ -415,21 +432,16 @@ impl DapHandler {
415432
416433 drop ( state) ;
417434
418- let rsp = req . success ( ResponseBody :: SetBreakpoints ( SetBreakpointsResponse {
419- breakpoints : response_breakpoints ,
420- } ) ) ;
421-
422- Ok ( DapOutput :: response ( rsp ) )
435+ Ok ( HandlerOutput :: new ( ResponseBody :: SetBreakpoints (
436+ SetBreakpointsResponse {
437+ breakpoints : response_breakpoints ,
438+ } ,
439+ ) ) )
423440 }
424441
425- fn handle_attach (
426- & self ,
427- req : Request ,
428- _args : AttachRequestArguments ,
429- ) -> anyhow:: Result < DapOutput > {
430- let rsp = req. success ( ResponseBody :: Attach ) ;
431- Ok ( DapOutput {
432- response : rsp,
442+ fn handle_attach ( & self , _args : AttachRequestArguments ) -> anyhow:: Result < HandlerOutput > {
443+ Ok ( HandlerOutput {
444+ body : ResponseBody :: Attach ,
433445 dap_events : vec ! [ Event :: Thread ( ThreadEventBody {
434446 reason: ThreadEventReason :: Started ,
435447 thread_id: THREAD_ID ,
@@ -438,11 +450,7 @@ impl DapHandler {
438450 } )
439451 }
440452
441- fn handle_disconnect (
442- & self ,
443- req : Request ,
444- _args : DisconnectArguments ,
445- ) -> anyhow:: Result < DapOutput > {
453+ fn handle_disconnect ( & self , _args : DisconnectArguments ) -> anyhow:: Result < HandlerOutput > {
446454 // Only send `Q` if currently in a debugging session.
447455 let is_debugging = { self . state . lock ( ) . unwrap ( ) . is_debugging } ;
448456 let console_events = if is_debugging {
@@ -451,58 +459,51 @@ impl DapHandler {
451459 vec ! [ ]
452460 } ;
453461
454- Ok ( DapOutput {
455- response : req . success ( ResponseBody :: Disconnect ) ,
462+ Ok ( HandlerOutput {
463+ body : ResponseBody :: Disconnect ,
456464 dap_events : vec ! [ ] ,
457465 console_events,
458466 } )
459467 }
460468
461- fn handle_restart < T > ( & self , req : Request , _args : T ) -> anyhow:: Result < DapOutput > {
462- Ok ( DapOutput {
463- response : req . success ( ResponseBody :: Restart ) ,
469+ fn handle_restart < T > ( & self , _args : T ) -> anyhow:: Result < HandlerOutput > {
470+ Ok ( HandlerOutput {
471+ body : ResponseBody :: Restart ,
464472 dap_events : vec ! [ ] ,
465473 console_events : vec ! [ DapConsoleEvent :: Restart ] ,
466474 } )
467475 }
468476
469477 // All servers must respond to `Threads` requests, possibly with
470478 // a dummy thread as is the case here
471- fn handle_threads ( & self , req : Request ) -> anyhow:: Result < DapOutput > {
472- let rsp = req . success ( ResponseBody :: Threads ( ThreadsResponse {
479+ fn handle_threads ( & self ) -> anyhow:: Result < HandlerOutput > {
480+ Ok ( HandlerOutput :: new ( ResponseBody :: Threads ( ThreadsResponse {
473481 threads : vec ! [ Thread {
474482 id: THREAD_ID ,
475483 name: String :: from( "R console" ) ,
476484 } ] ,
477- } ) ) ;
478- Ok ( DapOutput :: response ( rsp) )
485+ } ) ) )
479486 }
480487
481488 fn handle_set_exception_breakpoints (
482489 & self ,
483- req : Request ,
484490 args : SetExceptionBreakpointsArguments ,
485- ) -> anyhow:: Result < DapOutput > {
491+ ) -> anyhow:: Result < HandlerOutput > {
486492 {
487493 let mut state = self . state . lock ( ) . unwrap ( ) ;
488494 state. exception_breakpoint_filters = args. filters ;
489495 }
490- let rsp = req . success ( ResponseBody :: SetExceptionBreakpoints (
496+ Ok ( HandlerOutput :: new ( ResponseBody :: SetExceptionBreakpoints (
491497 SetExceptionBreakpointsResponse {
492498 // This field is only useful for reporting problems with
493499 // individual filters. Since we always accept all filters,
494500 // `None` is fine here.
495501 breakpoints : None ,
496502 } ,
497- ) ) ;
498- Ok ( DapOutput :: response ( rsp) )
503+ ) ) )
499504 }
500505
501- fn handle_stacktrace (
502- & self ,
503- req : Request ,
504- args : StackTraceArguments ,
505- ) -> anyhow:: Result < DapOutput > {
506+ fn handle_stacktrace ( & self , args : StackTraceArguments ) -> anyhow:: Result < HandlerOutput > {
506507 let stack = {
507508 let state = self . state . lock ( ) . unwrap ( ) ;
508509 let fallback_sources = & state. fallback_sources ;
@@ -540,19 +541,19 @@ impl DapHandler {
540541 } ;
541542 let stack = stack[ start..end] . to_vec ( ) ;
542543
543- let rsp = req . success ( ResponseBody :: StackTrace ( StackTraceResponse {
544- stack_frames : stack ,
545- total_frames : Some ( total_frames ) ,
546- } ) ) ;
547-
548- Ok ( DapOutput :: response ( rsp ) )
544+ Ok ( HandlerOutput :: new ( ResponseBody :: StackTrace (
545+ StackTraceResponse {
546+ stack_frames : stack ,
547+ total_frames : Some ( total_frames ) ,
548+ } ,
549+ ) ) )
549550 }
550551
551- fn handle_source ( & self , _req : Request , _args : SourceArguments ) -> anyhow:: Result < DapOutput > {
552+ fn handle_source ( & self , _args : SourceArguments ) -> anyhow:: Result < HandlerOutput > {
552553 Err ( anyhow:: anyhow!( "Unsupported `source` request" ) )
553554 }
554555
555- fn handle_scopes ( & self , req : Request , args : ScopesArguments ) -> anyhow:: Result < DapOutput > {
556+ fn handle_scopes ( & self , args : ScopesArguments ) -> anyhow:: Result < HandlerOutput > {
556557 let state = self . state . lock ( ) . unwrap ( ) ;
557558 let frame_id_to_variables_reference = & state. frame_id_to_variables_reference ;
558559
@@ -579,22 +580,19 @@ impl DapHandler {
579580 end_column: None ,
580581 } ] ;
581582
582- let rsp = req. success ( ResponseBody :: Scopes ( ScopesResponse { scopes } ) ) ;
583-
584583 drop ( state) ;
585- Ok ( DapOutput :: response ( rsp) )
584+ Ok ( HandlerOutput :: new ( ResponseBody :: Scopes ( ScopesResponse {
585+ scopes,
586+ } ) ) )
586587 }
587588
588- fn handle_variables (
589- & self ,
590- req : Request ,
591- args : VariablesArguments ,
592- ) -> anyhow:: Result < DapOutput > {
589+ fn handle_variables ( & self , args : VariablesArguments ) -> anyhow:: Result < HandlerOutput > {
593590 let variables_reference = args. variables_reference ;
594591 let variables = self . collect_r_variables ( variables_reference) ;
595592 let variables = self . make_variables ( variables) ;
596- let rsp = req. success ( ResponseBody :: Variables ( VariablesResponse { variables } ) ) ;
597- Ok ( DapOutput :: response ( rsp) )
593+ Ok ( HandlerOutput :: new ( ResponseBody :: Variables (
594+ VariablesResponse { variables } ,
595+ ) ) )
598596 }
599597
600598 fn collect_r_variables ( & self , variables_reference : i64 ) -> Vec < RVariable > {
@@ -626,25 +624,24 @@ impl DapHandler {
626624
627625 fn handle_step < A > (
628626 & self ,
629- req : Request ,
630627 _args : A ,
631628 cmd : DebugRequest ,
632629 resp : ResponseBody ,
633- ) -> anyhow:: Result < DapOutput > {
634- Ok ( DapOutput {
635- response : req . success ( resp) ,
630+ ) -> anyhow:: Result < HandlerOutput > {
631+ Ok ( HandlerOutput {
632+ body : resp,
636633 dap_events : vec ! [ ] ,
637634 console_events : vec ! [ DapConsoleEvent :: DebugCommand ( cmd) ] ,
638635 } )
639636 }
640637
641- fn handle_pause ( & self , req : Request , _args : PauseArguments ) -> anyhow:: Result < DapOutput > {
638+ fn handle_pause ( & self , _args : PauseArguments ) -> anyhow:: Result < HandlerOutput > {
642639 self . state . lock ( ) . unwrap ( ) . is_interrupting_for_debugger = true ;
643640
644641 log:: info!( "DAP: Received request to pause R, sending interrupt" ) ;
645642
646- Ok ( DapOutput {
647- response : req . success ( ResponseBody :: Pause ) ,
643+ Ok ( HandlerOutput {
644+ body : ResponseBody :: Pause ,
648645 dap_events : vec ! [ ] ,
649646 console_events : vec ! [ DapConsoleEvent :: Interrupt ] ,
650647 } )
0 commit comments