match on opts to define bodyref type

This commit is contained in:
mycognosist 2022-01-18 11:32:26 +02:00 committed by adria0.eth
parent 396abd6a60
commit 548fa0f850
1 changed files with 22 additions and 6 deletions

View File

@ -37,6 +37,14 @@ pub struct BodyRef<'a, T: serde::Serialize> {
pub args: &'a T, pub args: &'a T,
} }
#[derive(Serialize)]
pub struct BodyRefWithOpts<'a, T: serde::Serialize, U: serde::Serialize> {
pub name: &'a [&'a str],
#[serde(rename = "type")]
pub rpc_type: RpcType,
pub args: (&'a T, &'a U),
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq)] #[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq)]
pub enum RpcType { pub enum RpcType {
#[serde(rename = "async")] #[serde(rename = "async")]
@ -204,19 +212,27 @@ impl<W: io::Write + Unpin> RpcWriter<W> {
} }
} }
pub async fn send_request<T: serde::Serialize>( pub async fn send_request<T: serde::Serialize, U: serde::Serialize>(
&mut self, &mut self,
name: &[&str], name: &[&str],
rpc_type: RpcType, rpc_type: RpcType,
args: &T, args: &T,
opts: &Option<U>,
) -> Result<RequestNo> { ) -> Result<RequestNo> {
self.req_no += 1; self.req_no += 1;
let body_str = serde_json::to_string(&BodyRef { let body_str = match opts {
name, Some(options) => serde_json::to_string(&BodyRefWithOpts {
rpc_type, name,
args: &[&args], rpc_type,
})?; args: (args, options),
})?,
None => serde_json::to_string(&BodyRef {
name,
rpc_type,
args: &[args],
})?,
};
let rpc_header = Header { let rpc_header = Header {
req_no: self.req_no, req_no: self.req_no,