introduce ArgType enum for matching

This commit is contained in:
mycognosist 2022-02-09 10:24:31 +02:00 committed by adria0.eth
parent c8d86235c1
commit 47165ddc60
3 changed files with 38 additions and 13 deletions

View File

@ -4,7 +4,7 @@ use crate::{
TypedMessage, TypedMessage,
}, },
feed::Message, feed::Message,
rpc::{Body, BodyType, RequestNo, RpcType, RpcWriter}, rpc::{ArgType, Body, BodyType, RequestNo, RpcType, RpcWriter},
}; };
use async_std::io::Write; use async_std::io::Write;
@ -97,7 +97,9 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::InviteCreate.selector(), ApiMethod::InviteCreate.selector(),
RpcType::Async, RpcType::Async,
ArgType::Object,
&args, &args,
// specify None value for `opts`
&None::<()>, &None::<()>,
) )
.await?; .await?;
@ -111,6 +113,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::InviteUse.selector(), ApiMethod::InviteUse.selector(),
RpcType::Async, RpcType::Async,
ArgType::Array,
&invite_code, &invite_code,
&None::<()>, &None::<()>,
) )
@ -128,8 +131,8 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::FriendsIsFollowing.selector(), ApiMethod::FriendsIsFollowing.selector(),
RpcType::Async, RpcType::Async,
ArgType::Array,
&args, &args,
// specify None value for `opts`
&None::<()>, &None::<()>,
) )
.await?; .await?;
@ -146,8 +149,8 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::FriendsIsBlocking.selector(), ApiMethod::FriendsIsBlocking.selector(),
RpcType::Async, RpcType::Async,
ArgType::Array,
&args, &args,
// specify None value for `opts`
&None::<()>, &None::<()>,
) )
.await?; .await?;
@ -161,8 +164,8 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::FriendsHops.selector(), ApiMethod::FriendsHops.selector(),
RpcType::Source, RpcType::Source,
ArgType::Array,
&args, &args,
// specify None value for `opts`
&None::<()>, &None::<()>,
) )
.await?; .await?;
@ -180,6 +183,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::GetSubset.selector(), ApiMethod::GetSubset.selector(),
RpcType::Source, RpcType::Source,
ArgType::Tuple,
&query, &query,
&opts, &opts,
) )
@ -194,8 +198,8 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::Publish.selector(), ApiMethod::Publish.selector(),
RpcType::Async, RpcType::Async,
ArgType::Array,
&msg, &msg,
// specify None value for `opts`
&None::<()>, &None::<()>,
) )
.await?; .await?;
@ -218,6 +222,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::WhoAmI.selector(), ApiMethod::WhoAmI.selector(),
RpcType::Async, RpcType::Async,
ArgType::Array,
&args, &args,
&None::<()>, &None::<()>,
) )
@ -241,6 +246,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::Get.selector(), ApiMethod::Get.selector(),
RpcType::Async, RpcType::Async,
ArgType::Array,
&msg_id, &msg_id,
&None::<()>, &None::<()>,
) )
@ -271,6 +277,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::CreateHistoryStream.selector(), ApiMethod::CreateHistoryStream.selector(),
RpcType::Source, RpcType::Source,
ArgType::Array,
&args, &args,
&None::<()>, &None::<()>,
) )
@ -288,6 +295,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::CreateFeedStream.selector(), ApiMethod::CreateFeedStream.selector(),
RpcType::Source, RpcType::Source,
ArgType::Array,
&args, &args,
&None::<()>, &None::<()>,
) )
@ -303,6 +311,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::Latest.selector(), ApiMethod::Latest.selector(),
RpcType::Async, RpcType::Async,
ArgType::Array,
&args, &args,
&None::<()>, &None::<()>,
) )
@ -317,6 +326,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::BlobsGet.selector(), ApiMethod::BlobsGet.selector(),
RpcType::Source, RpcType::Source,
ArgType::Array,
&args, &args,
&None::<()>, &None::<()>,
) )
@ -340,6 +350,7 @@ impl<W: Write + Unpin> ApiCaller<W> {
.send_request( .send_request(
ApiMethod::BlobsCreateWants.selector(), ApiMethod::BlobsCreateWants.selector(),
RpcType::Source, RpcType::Source,
ArgType::Array,
&args, &args,
&None::<()>, &None::<()>,
) )

View File

@ -2,4 +2,4 @@ mod error;
mod stream; mod stream;
pub use error::{Error, Result}; pub use error::{Error, Result};
pub use stream::{Body, BodyType, RecvMsg, RequestNo, RpcReader, RpcType, RpcWriter}; pub use stream::{ArgType, Body, BodyType, RecvMsg, RequestNo, RpcReader, RpcType, RpcWriter};

View File

@ -14,6 +14,13 @@ const RPC_HEADER_STREAM_FLAG: u8 = 1 << 3;
const RPC_HEADER_END_OR_ERROR_FLAG: u8 = 1 << 2; const RPC_HEADER_END_OR_ERROR_FLAG: u8 = 1 << 2;
const RPC_HEADER_BODY_TYPE_MASK: u8 = 0b11; const RPC_HEADER_BODY_TYPE_MASK: u8 = 0b11;
#[derive(Debug)]
pub enum ArgType {
Array,
Tuple,
Object,
}
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum BodyType { pub enum BodyType {
Binary, Binary,
@ -216,22 +223,29 @@ impl<W: io::Write + Unpin> RpcWriter<W> {
&mut self, &mut self,
name: &[&str], name: &[&str],
rpc_type: RpcType, rpc_type: RpcType,
arg_type: ArgType,
args: &T, args: &T,
opts: &Option<U>, opts: &Option<U>,
) -> Result<RequestNo> { ) -> Result<RequestNo> {
self.req_no += 1; self.req_no += 1;
let body_str = match opts { // the arg_type allows us to package the args in the correct form
Some(options) => serde_json::to_string(&BodyRefWithOpts { let body_str = match arg_type {
name, ArgType::Array => serde_json::to_string(&BodyRef {
rpc_type,
args: (args, options),
})?,
None => serde_json::to_string(&BodyRef {
name, name,
rpc_type, rpc_type,
args: &[args], args: &[args],
})?, })?,
ArgType::Tuple => serde_json::to_string(&BodyRefWithOpts {
name,
rpc_type,
args: (args, opts),
})?,
ArgType::Object => serde_json::to_string(&BodyRef {
name,
rpc_type,
args: &args,
})?,
}; };
let rpc_header = Header { let rpc_header = Header {