-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proc-macros: Support deprecated methods for rpc client (#570)
* proc-macros: Fix documentation typo of `rpc_identifier` * proc-macros: Support deprecated methods for rpc client (#564) Calling a deprecated method of the RPC client should warn the user at compile-time. Extract the `#[deprecated]` macro as is while parsing the RpcMethod, and pass through the macro to the RPC client rendering. * tests/ui: Check deprecated method for rpc client (#564) To ensure that the test will fail during compilation, warnings are denied. Check that the deprecate macro will generate warnings just for the methods that are utilized.
- Loading branch information
Showing
4 changed files
with
100 additions
and
2 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
67 changes: 67 additions & 0 deletions
67
proc-macros/tests/ui/incorrect/rpc/rpc_deprecated_method.rs
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,67 @@ | ||
//! Test that calling a deprecated method will generate warnings at compile-time. | ||
// Treat warnings as errors to fail the build. | ||
#![deny(warnings)] | ||
|
||
use jsonrpsee::{ | ||
proc_macros::rpc, | ||
types::{async_trait, RpcResult}, | ||
ws_client::*, | ||
ws_server::WsServerBuilder, | ||
}; | ||
use std::net::SocketAddr; | ||
|
||
#[rpc(client, server)] | ||
pub trait Deprecated { | ||
// Deprecated method that is called by the client. | ||
#[deprecated(since = "0.5.0", note = "please use `new_method` instead")] | ||
#[method(name = "foo")] | ||
async fn async_method(&self) -> RpcResult<u8>; | ||
|
||
// Deprecated methods that are not called should not generate warnings. | ||
#[deprecated(since = "0.5.0", note = "please use `new_method` instead")] | ||
#[method(name = "foo_unused")] | ||
async fn async_method_unused(&self) -> RpcResult<u8>; | ||
|
||
// If the method is not marked as deprecated, should not generate warnings. | ||
#[method(name = "bar")] | ||
fn sync_method(&self) -> RpcResult<u8>; | ||
} | ||
|
||
pub struct DeprecatedServerImpl; | ||
|
||
#[async_trait] | ||
impl DeprecatedServer for DeprecatedServerImpl { | ||
async fn async_method(&self) -> RpcResult<u8> { | ||
Ok(16u8) | ||
} | ||
|
||
async fn async_method_unused(&self) -> RpcResult<u8> { | ||
Ok(32u8) | ||
} | ||
|
||
fn sync_method(&self) -> RpcResult<u8> { | ||
Ok(64u8) | ||
} | ||
} | ||
|
||
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(DeprecatedServerImpl.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(); | ||
|
||
// Calling this method should generate an warning. | ||
assert_eq!(client.async_method().await.unwrap(), 16); | ||
// Note: `async_method_unused` is not called, and should not generate warnings. | ||
assert_eq!(client.sync_method().await.unwrap(), 64); | ||
} |
12 changes: 12 additions & 0 deletions
12
proc-macros/tests/ui/incorrect/rpc/rpc_deprecated_method.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error: use of deprecated associated function `DeprecatedClient::async_method`: please use `new_method` instead | ||
--> $DIR/rpc_deprecated_method.rs:64:20 | ||
| | ||
64 | assert_eq!(client.async_method().await.unwrap(), 16); | ||
| ^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/rpc_deprecated_method.rs:4:9 | ||
| | ||
4 | #![deny(warnings)] | ||
| ^^^^^^^^ | ||
= note: `#[deny(deprecated)]` implied by `#[deny(warnings)]` |