@@ -22,6 +22,7 @@ use crate::{
2222 TeardownRequest , TeardownResponse ,
2323} ;
2424use async_trait:: async_trait;
25+ use serde:: de:: DeserializeOwned ;
2526use std:: collections:: HashMap ;
2627use tokio:: io:: { AsyncBufReadExt , AsyncWriteExt , BufReader } ;
2728use uuid:: Uuid ;
@@ -196,30 +197,34 @@ impl<H: Handler> Server<H> {
196197 result
197198 }
198199
199- async fn handle_setup ( & mut self , request : & serde_json:: Value , id : serde_json:: Value ) -> serde_json:: Value {
200- let params = match request. get ( "params" ) {
201- Some ( p) => p,
202- None => {
203- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
204- id,
205- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , "Missing params" ) ,
206- ) )
207- . unwrap_or ( serde_json:: json!( { } ) ) ;
208- }
209- } ;
200+ /// Parse and deserialize the `params` field from a JSON-RPC request.
201+ fn parse_params < T : DeserializeOwned > (
202+ request : & serde_json:: Value ,
203+ id : & serde_json:: Value ,
204+ ) -> std:: result:: Result < T , serde_json:: Value > {
205+ let params = request. get ( "params" ) . ok_or_else ( || {
206+ serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
207+ id. clone ( ) ,
208+ JsonRpcError :: new ( error_codes:: INVALID_PARAMS , "Missing params" ) ,
209+ ) )
210+ . unwrap_or ( serde_json:: json!( { } ) )
211+ } ) ?;
210212
211- let setup_request: SetupRequest = match serde_json:: from_value ( params. clone ( ) ) {
212- Ok ( req) => req,
213- Err ( e) => {
214- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
215- id,
216- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , format ! ( "Invalid params: {e}" ) ) ,
217- ) )
218- . unwrap_or ( serde_json:: json!( { } ) ) ;
219- }
220- } ;
213+ serde_json:: from_value ( params. clone ( ) ) . map_err ( |e| {
214+ serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
215+ id. clone ( ) ,
216+ JsonRpcError :: new ( error_codes:: INVALID_PARAMS , format ! ( "Invalid params: {e}" ) ) ,
217+ ) )
218+ . unwrap_or ( serde_json:: json!( { } ) )
219+ } )
220+ }
221221
222- match self . handler . setup ( setup_request. run_id , setup_request. datasets ) . await {
222+ /// Convert a handler result into a JSON-RPC response value.
223+ fn handler_response < T : serde:: Serialize > (
224+ result : std:: result:: Result < T , String > ,
225+ id : serde_json:: Value ,
226+ ) -> serde_json:: Value {
227+ match result {
223228 Ok ( response) => serde_json:: to_value ( JsonRpcResponse :: success ( id, response) )
224229 . unwrap_or ( serde_json:: json!( { } ) ) ,
225230 Err ( e) => serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
@@ -230,106 +235,36 @@ impl<H: Handler> Server<H> {
230235 }
231236 }
232237
233- async fn handle_query_method ( & mut self , request : & serde_json:: Value , id : serde_json:: Value ) -> serde_json:: Value {
234- let params = match request. get ( "params" ) {
235- Some ( p) => p,
236- None => {
237- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
238- id,
239- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , "Missing params" ) ,
240- ) )
241- . unwrap_or ( serde_json:: json!( { } ) ) ;
242- }
238+ async fn handle_setup ( & mut self , request : & serde_json:: Value , id : serde_json:: Value ) -> serde_json:: Value {
239+ let req: SetupRequest = match Self :: parse_params ( request, & id) {
240+ Ok ( r) => r,
241+ Err ( e) => return e,
243242 } ;
243+ Self :: handler_response ( self . handler . setup ( req. run_id , req. datasets ) . await , id)
244+ }
244245
245- let query_request: QueryMethodRequest = match serde_json:: from_value ( params. clone ( ) ) {
246- Ok ( req) => req,
247- Err ( e) => {
248- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
249- id,
250- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , format ! ( "Invalid params: {e}" ) ) ,
251- ) )
252- . unwrap_or ( serde_json:: json!( { } ) ) ;
253- }
246+ async fn handle_query_method ( & mut self , request : & serde_json:: Value , id : serde_json:: Value ) -> serde_json:: Value {
247+ let req: QueryMethodRequest = match Self :: parse_params ( request, & id) {
248+ Ok ( r) => r,
249+ Err ( e) => return e,
254250 } ;
255-
256- match self . handler . query_method ( query_request. run_id ) . await {
257- Ok ( response) => serde_json:: to_value ( JsonRpcResponse :: success ( id, response) )
258- . unwrap_or ( serde_json:: json!( { } ) ) ,
259- Err ( e) => serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
260- id,
261- JsonRpcError :: new ( error_codes:: INTERNAL_ERROR , e) ,
262- ) )
263- . unwrap_or ( serde_json:: json!( { } ) ) ,
264- }
251+ Self :: handler_response ( self . handler . query_method ( req. run_id ) . await , id)
265252 }
266253
267254 async fn handle_teardown ( & mut self , request : & serde_json:: Value , id : serde_json:: Value ) -> serde_json:: Value {
268- let params = match request. get ( "params" ) {
269- Some ( p) => p,
270- None => {
271- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
272- id,
273- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , "Missing params" ) ,
274- ) )
275- . unwrap_or ( serde_json:: json!( { } ) ) ;
276- }
277- } ;
278-
279- let teardown_request: TeardownRequest = match serde_json:: from_value ( params. clone ( ) ) {
280- Ok ( req) => req,
281- Err ( e) => {
282- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
283- id,
284- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , format ! ( "Invalid params: {e}" ) ) ,
285- ) )
286- . unwrap_or ( serde_json:: json!( { } ) ) ;
287- }
255+ let req: TeardownRequest = match Self :: parse_params ( request, & id) {
256+ Ok ( r) => r,
257+ Err ( e) => return e,
288258 } ;
289-
290- match self . handler . teardown ( teardown_request. run_id ) . await {
291- Ok ( response) => serde_json:: to_value ( JsonRpcResponse :: success ( id, response) )
292- . unwrap_or ( serde_json:: json!( { } ) ) ,
293- Err ( e) => serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
294- id,
295- JsonRpcError :: new ( error_codes:: INTERNAL_ERROR , e) ,
296- ) )
297- . unwrap_or ( serde_json:: json!( { } ) ) ,
298- }
259+ Self :: handler_response ( self . handler . teardown ( req. run_id ) . await , id)
299260 }
300261
301262 async fn handle_metrics ( & mut self , request : & serde_json:: Value , id : serde_json:: Value ) -> serde_json:: Value {
302- let params = match request. get ( "params" ) {
303- Some ( p) => p,
304- None => {
305- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
306- id,
307- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , "Missing params" ) ,
308- ) )
309- . unwrap_or ( serde_json:: json!( { } ) ) ;
310- }
263+ let req: MetricsRequest = match Self :: parse_params ( request, & id) {
264+ Ok ( r) => r,
265+ Err ( e) => return e,
311266 } ;
312-
313- let metrics_request: MetricsRequest = match serde_json:: from_value ( params. clone ( ) ) {
314- Ok ( req) => req,
315- Err ( e) => {
316- return serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
317- id,
318- JsonRpcError :: new ( error_codes:: INVALID_PARAMS , format ! ( "Invalid params: {e}" ) ) ,
319- ) )
320- . unwrap_or ( serde_json:: json!( { } ) ) ;
321- }
322- } ;
323-
324- match self . handler . metrics ( metrics_request. run_id ) . await {
325- Ok ( response) => serde_json:: to_value ( JsonRpcResponse :: success ( id, response) )
326- . unwrap_or ( serde_json:: json!( { } ) ) ,
327- Err ( e) => serde_json:: to_value ( JsonRpcResponse :: < ( ) > :: error (
328- id,
329- JsonRpcError :: new ( error_codes:: INTERNAL_ERROR , e) ,
330- ) )
331- . unwrap_or ( serde_json:: json!( { } ) ) ,
332- }
267+ Self :: handler_response ( self . handler . metrics ( req. run_id ) . await , id)
333268 }
334269
335270 async fn handle_rpc_methods ( & mut self , id : serde_json:: Value ) -> serde_json:: Value {
0 commit comments