-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(proc_macro): add support for map arguments * feat(proc_macro): formatting * feat(proc_macro): fix issues with Into trait * feat(proc_macro): param_format for methods * feat(proc_macro): improve param_format checking - Addressed @niklasad1's suggestion to use an Option instead of just defaulting to "array". * feat(proc_macro): apply suggestions, add test case - Use enum for param format. - Extract parsing logic into separate function. - Add ui test. * feat(proc_macro): run cargo fmt * feat(proc_macro): address suggestions * feat(proc_macro): document param_kind argument * feat(proc_macro): consistent spacing Apply @maciejhirsz formatting suggestion. Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com> * feat(proc_macro): apply suggestions - make parameter encoding DRY - remove strings from param_kind - return result from parse_param_kind * feat(proc_macro): formatting Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com>
- Loading branch information
1 parent
092081a
commit ff3337b
Showing
7 changed files
with
155 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use jsonrpsee::{ | ||
proc_macros::rpc, | ||
types::{async_trait, RpcResult}, | ||
ws_client::*, | ||
ws_server::WsServerBuilder, | ||
}; | ||
|
||
use std::net::SocketAddr; | ||
|
||
#[rpc(client, server, namespace = "foo")] | ||
pub trait Rpc { | ||
#[method(name = "method_with_array_param", param_kind = array)] | ||
async fn method_with_array_param(&self, param_a: u8, param_b: String) -> RpcResult<u16>; | ||
|
||
#[method(name="method_with_map_param", param_kind= map)] | ||
async fn method_with_map_param(&self, param_a: u8, param_b: String) -> RpcResult<u16>; | ||
|
||
#[method(name="method_with_default_param")] | ||
async fn method_with_default_param(&self, param_a: u8, param_b: String) -> RpcResult<u16>; | ||
} | ||
|
||
pub struct RpcServerImpl; | ||
|
||
#[async_trait] | ||
impl RpcServer for RpcServerImpl { | ||
async fn method_with_array_param(&self, param_a: u8, param_b: String) -> RpcResult<u16> { | ||
assert_eq!(param_a, 0); | ||
assert_eq!(¶m_b, "a"); | ||
Ok(42u16) | ||
} | ||
|
||
async fn method_with_map_param(&self, param_a: u8, param_b: String) -> RpcResult<u16> { | ||
assert_eq!(param_a, 0); | ||
assert_eq!(¶m_b, "a"); | ||
Ok(42u16) | ||
} | ||
|
||
async fn method_with_default_param(&self, param_a: u8, param_b: String) -> RpcResult<u16> { | ||
assert_eq!(param_a, 0); | ||
assert_eq!(¶m_b, "a"); | ||
Ok(42u16) | ||
} | ||
} | ||
|
||
pub async fn websocket_server() -> SocketAddr { | ||
let server = WsServerBuilder::default().build("127.0.0.1:0").await.unwrap(); | ||
let addr = server.local_addr().unwrap(); | ||
|
||
server.start(RpcServerImpl.into_rpc()).unwrap(); | ||
|
||
addr | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let server_addr = websocket_server().await; | ||
let server_url = format!("ws://{}", server_addr); | ||
let client = WsClientBuilder::default().build(&server_url).await.unwrap(); | ||
|
||
assert_eq!(client.method_with_array_param(0, "a".into()).await.unwrap(), 42); | ||
assert_eq!(client.method_with_map_param(0, "a".into()).await.unwrap(), 42); | ||
assert_eq!(client.method_with_default_param(0, "a".into()).await.unwrap(), 42); | ||
} |
4 changes: 2 additions & 2 deletions
4
proc-macros/tests/ui/incorrect/method/method_unexpected_field.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: Unknown argument `magic`, expected one of: `aliases`, `blocking`, `name`, `resources` | ||
--> $DIR/method_unexpected_field.rs:6:25 | ||
error: Unknown argument `magic`, expected one of: `aliases`, `blocking`, `name`, `param_kind`, `resources` | ||
--> tests/ui/incorrect/method/method_unexpected_field.rs:6:25 | ||
| | ||
6 | #[method(name = "foo", magic = false)] | ||
| ^^^^^ |
4 changes: 2 additions & 2 deletions
4
proc-macros/tests/ui/incorrect/sub/sub_unsupported_field.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: Unknown argument `magic`, expected one of: `aliases`, `item`, `name`, `unsubscribe_aliases` | ||
--> $DIR/sub_unsupported_field.rs:6:42 | ||
error: Unknown argument `magic`, expected one of: `aliases`, `item`, `name`, `param_kind`, `unsubscribe_aliases` | ||
--> tests/ui/incorrect/sub/sub_unsupported_field.rs:6:42 | ||
| | ||
6 | #[subscription(name = "sub", item = u8, magic = true)] | ||
| ^^^^^ |
ff3337b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
2
.subscriptions/unsub
2042
ns/iter (± 445
)895
ns/iter (± 208
)2.28
This comment was automatically generated by workflow using github-action-benchmark.
CC: @niklasad1 @maciejhirsz