pub struct Methods { /* private fields */ }Expand description
Registry of JSON-RPC methods with a builder pattern.
Methods allows you to register JSON-RPC method handlers using a fluent
builder API. The registered methods can then be passed to the serve function
to start a JSON-RPC server.
§Example
use json_rpc::Methods;
async fn echo(params: serde_json::Value) -> Result<serde_json::Value, json_rpc::Error> {
Ok(params)
}
let methods = Methods::new()
.add("echo", echo);Implementations§
Source§impl Methods
impl Methods
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::Methods;
use serde_json::Value;
async fn add(params: (i32, i32)) -> Result<i32, json_rpc::Error> {
Ok(params.0 + params.1)
}
let methods = Methods::new()
.add("add", add);Sourcepub async fn process_message(&self, json_str: &str) -> Option<String>
pub async fn process_message(&self, json_str: &str) -> Option<String>
Process a JSON-RPC message and return the response JSON string (if any).
This helper method is used by transport implementations to process incoming JSON-RPC messages. It handles:
- JSON parsing and validation
- Message type detection (request, notification, batch, response)
- Method routing and execution
- Error handling and response generation
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Methods
impl !RefUnwindSafe for Methods
impl Send for Methods
impl Sync for Methods
impl Unpin for Methods
impl UnsafeUnpin for Methods
impl !UnwindSafe for Methods
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