Implement a typed version of call to avoid useless matching

This commit is contained in:
elsirion
2022-05-13 19:43:36 +00:00
committed by Christian Decker
parent 7046252f96
commit 10917743fe
2 changed files with 74 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ pub use crate::{
notifications::Notification,
primitives::RpcError,
};
use crate::model::IntoRequest;
///
pub struct ClnRpc {
@@ -105,6 +106,13 @@ impl ClnRpc {
})
}
}
pub async fn call_typed<R: IntoRequest>(&mut self, request: R) -> Result<R::Response, RpcError> {
Ok(self.call(request.into())
.await?
.try_into()
.expect("CLN will reply correctly"))
}
}
/// Used to skip optional arrays when serializing requests.
@@ -142,4 +150,23 @@ mod test {
read_req
);
}
#[tokio::test]
async fn test_typed_call() {
let req = requests::GetinfoRequest {};
let (uds1, uds2) = UnixStream::pair().unwrap();
let mut cln = ClnRpc::from_stream(uds1).unwrap();
let mut read = FramedRead::new(uds2, JsonCodec::default());
tokio::task::spawn(async move {
let _: GetinfoResponse = cln.call_typed(req).await.unwrap();
});
let read_req = dbg!(read.next().await.unwrap().unwrap());
assert_eq!(
json!({"id": 1, "method": "getinfo", "params": {}, "jsonrpc": "2.0"}),
read_req
);
}
}