fn rpc_proxy(
  js: String,
  handler: &dyn Fn(RpcRequest) -> Option<RpcResponse>,
) -> Result<Option<String>> {
  let req = serde_json::from_str::<RpcRequest>(&js)
    .map_err(|e| Error::RpcScriptError(e.to_string(), js))?;

  let mut response = (handler)(req);
  // Got a synchronous response so convert it to a script to be evaluated
  if let Some(mut response) = response.take() {
    if let Some(id) = response.id {
      let js = if let Some(error) = response.error.take() {
        RpcResponse::into_error_script(id, error)?
      } else if let Some(result) = response.result.take() {
        RpcResponse::into_result_script(id, result)?
      } else {
        // No error or result, assume a positive response
        // with empty result (ACK)
        RpcResponse::into_result_script(id, Value::Null)?
      };
      Ok(Some(js))
    } else {
      Ok(None)
    }
  } else {
    Ok(None)
  }
}
// Helper so all platforms handle RPC messages consistently.
Helper so all platforms handle RPC messages consistently.
Helper so all platforms handle RPC messages consistently .
fn rpc_proxy(
  js: String,
  handler: &dyn Fn(RpcRequest) -> Option<RpcResponse>,
) -> Result<Option<String>> {
  let req = serde_json::from_str::<RpcRequest>(&js)
    .map_err(|e| Error::RpcScriptError(e.to_string(), js))?;
  let mut response = (handler)(req);
  // Got a synchronous response so convert it to a script to be evaluated
  if let Some(mut response) = response.take() {
    if let Some(id) = response.id {
      let js = if let Some(error) = response.error.take() {
        RpcResponse::into_error_script(id, error)?
      } else if let Some(result) = response.result.take() {
        RpcResponse::into_result_script(id, result)?
      } else {
        // No error or result, assume a positive response
        // with empty result (ACK)
        RpcResponse::into_result_script(id, Value::Null)?
      };
      Ok(Some(js))
    } else {
      Ok(None)
    }
  } else {
    Ok(None)
  }
}
fn rpc_proxy ( js : String , handler : & dyn Fn ( RpcRequest ) -> Option < RpcResponse > , ) -> Result < Option < String > > { let req = serde_json :: from_str :: < RpcRequest > ( & js ) . map_err ( | e | Error :: RpcScriptError ( e . to_string ( ) , js ) ) ? ; let mut response = ( handler ) ( req ) ; // Got a synchronous response so convert it to a script to be evaluated if let Some ( mut response ) = response . take ( ) { if let Some ( id ) = response . id { let js = if let Some ( error ) = response . error . take ( ) { RpcResponse :: into_error_script ( id , error ) ? } else if let Some ( result ) = response . result . take ( ) { RpcResponse :: into_result_script ( id , result ) ? } else { // No error or result, assume a positive response // with empty result (ACK) RpcResponse :: into_result_script ( id , Value :: Null ) ? } ; Ok ( Some ( js ) ) } else { Ok ( None ) } } else { Ok ( None ) } }
Helper so all platforms handle RPC messages consistently.
Helper so all platforms handle RPC messages consistently .
js
String
handler
&dyn Fn(RpcRequest) -> Option<RpcResponse>
js
String
handler
&dyn Fn(RpcRequest) -> Option<RpcResponse>