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

View File

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