pub struct JsonRpc { /* private fields */ }Expand description
JSON-RPC handler for message processing.
JsonRpc registers method handlers and processes JSON-RPC messages via the
call() method. Use the builder pattern to add methods with automatic
parameter deserialization.
§Example
use json_rpc::JsonRpc;
async fn echo(params: serde_json::Value) -> Result<serde_json::Value, json_rpc::Error> {
Ok(params)
}
let json_rpc = JsonRpc::new()
.add("echo", echo);
let response = json_rpc.call(r#"{"jsonrpc":"2.0","method":"echo","params":"hello","id":1}"#).await;Implementations§
Source§impl JsonRpc
impl JsonRpc
Sourcepub fn add<F, P, R, Fut>(self, method: &str, handler: F) -> Self
pub fn add<F, P, R, Fut>(self, method: &str, handler: F) -> Self
Register a JSON-RPC method handler.
The handler must be an async function that takes deserialized parameters
and returns a Result with either the return value or an Error.
§Example
use json_rpc::JsonRpc;
use serde_json::Value;
async fn add(params: (i32, i32)) -> Result<i32, json_rpc::Error> {
Ok(params.0 + params.1)
}
let json_rpc = JsonRpc::new()
.add("add", add);Sourcepub async fn call(&self, json_str: &str) -> Option<String>
pub async fn call(&self, json_str: &str) -> Option<String>
Process a JSON-RPC message and return the response JSON string (if any).
This method processes a JSON-RPC message string and returns the response. It handles:
- JSON parsing and validation
- Message type detection (request, notification, batch, response)
- Method routing and execution
- Error handling and response generation
Returns None for notifications (which don’t require a response).
Auto Trait Implementations§
impl Freeze for JsonRpc
impl !RefUnwindSafe for JsonRpc
impl Send for JsonRpc
impl Sync for JsonRpc
impl Unpin for JsonRpc
impl UnsafeUnpin for JsonRpc
impl !UnwindSafe for JsonRpc
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more