| 551 | } |
| 552 | |
| 553 | pub fn new_typed<C, F, Req, Resp>(name: &str, callback: C) -> Self |
| 554 | where |
| 555 | C: Send + Sync + 'static, |
| 556 | C: Fn(Plugin<S>, Req) -> F + 'static, |
| 557 | F: Future<Output = Result<Resp, Error>> + Send + 'static, |
| 558 | Req: DeserializeOwned + Send + 'static, |
| 559 | Resp: Serialize + Send + 'static, |
| 560 | { |
| 561 | let hookname = name.to_string(); |
| 562 | Self { |
| 563 | name: hookname.clone(), |
| 564 | callback: Box::new(move |p, r| { |
| 565 | let hookname = hookname.clone(); |
| 566 | let typed_req = match serde_json::from_value(r) { |
| 567 | Ok(req) => req, |
| 568 | Err(e) => { |
| 569 | let error = format!( |
| 570 | "cln-plugin: hook '{hookname}' received a request that doesn't match \ |
| 571 | the expected schema. Error: {e}" |
| 572 | ); |
| 573 | log::warn!("{error}"); |
| 574 | return Box::pin(async move { Err(anyhow!("{error}")) }); |
| 575 | } |
| 576 | }; |
| 577 | let fut = callback(p, typed_req); |
| 578 | Box::pin(async move { |
| 579 | let typed_resp = fut.await?; |
| 580 | serde_json::to_value(typed_resp).map_err(Error::from) |
| 581 | }) |
| 582 | }), |
| 583 | before: Vec::new(), |
| 584 | after: Vec::new(), |
| 585 | filters: None, |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | pub fn before(mut self, before: Vec<String>) -> Self { |
| 590 | self.before = before; |