Skip to content

Commit

Permalink
fix(proc-macros): re-export tokio for params parsing (#1361)
Browse files Browse the repository at this point in the history
* fix(proc-macros): re-export tokio and tests

* Update tests/proc-macro-core/src/lib.rs

* Update tests/proc-macro-core/Cargo.toml

* Update core/src/lib.rs

* Update tests/proc-macro-core/src/lib.rs

* tests: custom params types

* unify params
  • Loading branch information
niklasad1 authored Apr 29, 2024
1 parent b2ed47b commit c908eeb
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"test-utils",
"tests",
"tests/wasm-tests",
"tests/proc-macro-core",
"types",
]
resolver = "2"
Expand Down
5 changes: 5 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ pub mod __reexports {
pub use async_trait::async_trait;
pub use serde;
pub use serde_json;

// Needed for the params parsing in the proc macro API.
cfg_client_or_server! {
pub use tokio;
}
}

pub use beef::Cow;
Expand Down
9 changes: 9 additions & 0 deletions core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ macro_rules! cfg_server {
};
}

macro_rules! cfg_client_or_server {
($($item:item)*) => {
$(
#[cfg(any(feature = "client", feature = "server"))]
$item
)*
}
}

macro_rules! cfg_http_helpers {
($($item:item)*) => {
cfg_feature!("http-helpers", $($item)*);
Expand Down
2 changes: 1 addition & 1 deletion proc-macros/src/render_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl RpcDescription {
let tracing = self.jrps_server_item(quote! { tracing });
let sub_err = self.jrps_server_item(quote! { SubscriptionCloseResponse });
let response_payload = self.jrps_server_item(quote! { ResponsePayload });
let tokio = self.jrps_server_item(quote! { tokio });
let tokio = self.jrps_server_item(quote! { core::__reexports::tokio });

// Code to decode sequence of parameters from a JSON array.
let decode_array = {
Expand Down
12 changes: 12 additions & 0 deletions tests/proc-macro-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "jsonrpsee-proc-macro-core"
description = "Test crate for the proc-macro API to make sure that it compiles with the core features"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
publish = false

[dependencies]
jsonrpsee = { path = "../../jsonrpsee", features = ["server-core", "client-core", "macros"] }
serde = "1.0"
73 changes: 73 additions & 0 deletions tests/proc-macro-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Test module for the proc-macro API to make sure that it compiles with the core features.
use jsonrpsee::core::{async_trait, SubscriptionResult};
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::types::ErrorObjectOwned;
use jsonrpsee::{ConnectionDetails, PendingSubscriptionSink, SubscriptionMessage};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub enum PubSubKind {
A,
B,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PubSubParams {
params: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PubSubItem {
result: String,
}

#[rpc(client, server)]
pub trait Api {
#[method(name = "sync_call")]
fn sync_call(&self, a: String) -> Result<String, ErrorObjectOwned>;

#[method(name = "async_call")]
async fn async_call(&self, a: String) -> Result<String, ErrorObjectOwned>;

#[subscription(name = "subscribe", item = PubSubItem)]
async fn sub(&self, kind: PubSubKind, p: PubSubParams) -> SubscriptionResult;

#[subscription(name = "subscribeSync", item = String)]
fn sync_sub(&self, a: String) -> SubscriptionResult;

#[method(name = "blocking", blocking)]
fn blocking_method(&self, a: String) -> Result<u16, ErrorObjectOwned>;

#[method(name = "raw", raw_method)]
async fn raw(&self, a: String) -> Result<u16, ErrorObjectOwned>;
}

#[async_trait]
impl ApiServer for () {
fn sync_call(&self, _: String) -> Result<String, ErrorObjectOwned> {
Ok("sync_call".to_string())
}

async fn async_call(&self, _: String) -> Result<String, ErrorObjectOwned> {
Ok("async_call".to_string())
}

async fn sub(&self, pending: PendingSubscriptionSink, _: PubSubKind, _: PubSubParams) -> SubscriptionResult {
let sink = pending.accept().await?;
sink.send(SubscriptionMessage::from("msg")).await?;
Ok(())
}

fn sync_sub(&self, _: PendingSubscriptionSink, _: String) -> SubscriptionResult {
Ok(())
}

fn blocking_method(&self, _: String) -> Result<u16, ErrorObjectOwned> {
Ok(42)
}

async fn raw(&self, _: ConnectionDetails, _: String) -> Result<u16, ErrorObjectOwned> {
Ok(42)
}
}

0 comments on commit c908eeb

Please sign in to comment.