Attribute Macro jsonrpc_utils::rpc
source · #[rpc]
Available on crate feature
macros
only.Expand description
Using a rust trait to define RPC methods.
Example
#[rpc]
#[async_trait]
trait MyRpc {
async fn sleep(&self, x: u64) -> Result<u64>;
// Tailing optional parameters are optional.
async fn value(&self, x: Option<u64>) -> Result<u64>;
async fn add(&self, (x, y): (i32, i32), z: i32) -> Result<i32>;
// Override rpc method name.
#[rpc(name = "@ping")]
fn ping(&self) -> Result<String>;
type S: Stream<Item = PublishMsg<u64>> + Send + 'static;
#[rpc(pub_sub(notify = "subscription", unsubscribe = "unsubscribe"))]
fn subscribe(&self, interval: u64) -> Result<Self::S>;
}
#[async_trait]
impl MyRpc for MyRpcImpl {...}
let mut rpc = MetaIoHandler::with_compatibility(jsonrpc_core::Compatibility::V2);
add_my_rpc_methods(&mut rpc, RpcImpl);
Error handling
The error type must implement Into<jsonrpc_core::Error>
.
OpenRPC doc generation
Using #[rpc(openrpc)]
on e.g. trait MyRpc
will generate a my_rpc_doc
function, which will return an OpenRPC document for the RPC methods as a
serde_json::Value
.
All parameters and results must implement the JsonSchema trait.
Pub/sub methods won’t be included in the generated doc for now.
Full example: openrpc.rs