(&mut self, method: &'static str, handler: F)
| 75 | } |
| 76 | |
| 77 | pub fn register_with_context<P, R, F, Fut>(&mut self, method: &'static str, handler: F) |
| 78 | where |
| 79 | P: DeserializeOwned + Send + 'static, |
| 80 | R: Serialize + Send + 'static, |
| 81 | F: Fn(RequestContext, P) -> Fut + Send + Sync + Clone + 'static, |
| 82 | Fut: Future<Output = Result<R, RpcError>> + Send + 'static, |
| 83 | { |
| 84 | let boxed: BoxedHandler = Box::new(move |ctx: &RequestContext, params: &[u8]| { |
| 85 | let handler = handler.clone(); |
| 86 | let ctx = ctx.clone(); |
| 87 | let params: Result<P, _> = serde_json::from_slice(params); |
| 88 | Box::pin(async move { |
| 89 | let params = params.map_err(|e| RpcError::invalid_params(e))?; |
| 90 | let result = handler(ctx, params).await?; |
| 91 | serde_json::to_value(&result).map_err(|_| RpcError::internal_error("parsing error")) |
| 92 | }) |
| 93 | }); |
| 94 | self.handlers.insert(method, boxed); |
| 95 | } |
| 96 | |
| 97 | pub fn build(self) -> JsonRpcRouter { |
| 98 | JsonRpcRouter { |
nothing calls this directly
no test coverage detected