Enum JsonRpc

Source
pub enum JsonRpc {
    Request(Request),
    Notification(Notification),
    Success(Success),
    Error(Error),
}
Expand description

JSON-RPC 2.0 Request object and Response object JSON-RPC 2.0 Specification.

This enum represents all possible JSON-RPC message types:

  • Request: A method call with an identifier
  • Notification: A method call without an identifier (no response expected)
  • Success: A successful response to a request
  • Error: An error response to a request

Variants§

§

Request(Request)

Request object

§

Notification(Notification)

Notification object

§

Success(Success)

Success Response

§

Error(Error)

Error Response

Implementations§

Source§

impl JsonRpc

Source

pub fn request<I: Into<Id>>(id: I, method: &str) -> Self

Creates a JSON-RPC 2.0 request object without params

§Arguments
  • id - The identifier for the request
  • method - The name of the method to call
§Returns

A new JsonRpc::Request variant

§Examples
use jsonrpc_lite::JsonRpc;

let request = JsonRpc::request(1, "echo");
Source

pub fn request_with_params<I: Into<Id>, P: Into<Params>>( id: I, method: &str, params: P, ) -> Self

Creates a JSON-RPC 2.0 request object with params

§Arguments
  • id - The identifier for the request
  • method - The name of the method to call
  • params - The parameters to pass to the method
§Returns

A new JsonRpc::Request variant with parameters

§Examples
use jsonrpc_lite::JsonRpc;
use serde_json::json;

let request = JsonRpc::request_with_params(1, "add", json!([1, 2]));
Source

pub fn notification(method: &str) -> Self

Creates a JSON-RPC 2.0 notification object without params

§Arguments
  • method - The name of the method to call
§Returns

A new JsonRpc::Notification variant

§Examples
use jsonrpc_lite::JsonRpc;

let notification = JsonRpc::notification("ping");
Source

pub fn notification_with_params<P: Into<Params>>( method: &str, params: P, ) -> Self

Creates a JSON-RPC 2.0 notification object with params

§Arguments
  • method - The name of the method to call
  • params - The parameters to pass to the method
§Returns

A new JsonRpc::Notification variant with parameters

§Examples
use jsonrpc_lite::JsonRpc;
use serde_json::json;

let notification = JsonRpc::notification_with_params("log", json!({"level": "info", "message": "Hello"}));
Source

pub fn success<I: Into<Id>>(id: I, result: &Value) -> Self

Creates a JSON-RPC 2.0 success response object

§Arguments
  • id - The identifier matching the request
  • result - The result of the method call
§Returns

A new JsonRpc::Success variant

§Examples
use jsonrpc_lite::JsonRpc;
use serde_json::json;

let response = JsonRpc::success(1, &json!(42));
Source

pub fn error<I: Into<Id>>(id: I, error: RpcError) -> Self

Creates a JSON-RPC 2.0 error response object

§Arguments
  • id - The identifier matching the request
  • error - The error that occurred
§Returns

A new JsonRpc::Error variant

§Examples
use jsonrpc_lite::{JsonRpc, Error};

let response = JsonRpc::error(1, Error::method_not_found());
Source

pub fn get_version(&self) -> Option<&str>

Gets the JSON-RPC protocol version

§Returns

The protocol version string (“2.0”) or None if not available

Source

pub fn get_id(&self) -> Option<Id>

Gets the identifier from the JSON-RPC message

§Returns

The identifier if present (for requests and responses), or None for notifications

Source

pub fn get_method(&self) -> Option<&str>

Gets the method name from the JSON-RPC message

§Returns

The method name if present (for requests and notifications), or None for responses

Source

pub fn get_params(&self) -> Option<Params>

Gets the parameters from the JSON-RPC message

§Returns

The parameters if present (for requests and notifications), or None for responses

Source

pub fn get_result(&self) -> Option<&Value>

Gets the result from a successful JSON-RPC response

§Returns

The result if this is a success response, or None otherwise

Source

pub fn get_error(&self) -> Option<&RpcError>

Gets the error from an error JSON-RPC response

§Returns

The error if this is an error response, or None otherwise

Source

pub fn parse(input: &str) -> SerdeResult<Self>

Parses a JSON string into a JSON-RPC message

§Arguments
  • input - The JSON string to parse
§Returns

A Result containing either the parsed JsonRpc or a serde_json error

§Examples
use jsonrpc_lite::JsonRpc;

let input = r#"{"jsonrpc":"2.0","method":"subtract","params":[42,23],"id":1}"#;
let request = JsonRpc::parse(input).unwrap();
Source

pub fn parse_vec(input: &str) -> SerdeResult<Vec<Self>>

Parses a JSON string into a vector of JSON-RPC messages

This is useful for batch requests and responses.

§Arguments
  • input - The JSON string to parse
§Returns

A Result containing either a vector of parsed JsonRpc objects or a serde_json error

§Examples
use jsonrpc_lite::JsonRpc;

let input = r#"[{"jsonrpc":"2.0","method":"sum","params":[1,2,4],"id":"1"},{"jsonrpc":"2.0","method":"notify_hello","params":[7]}]"#;
let batch = JsonRpc::parse_vec(input).unwrap();
assert_eq!(batch.len(), 2);

Trait Implementations§

Source§

impl Clone for JsonRpc

Source§

fn clone(&self) -> JsonRpc

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JsonRpc

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for JsonRpc

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for JsonRpc

Source§

fn eq(&self, other: &JsonRpc) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for JsonRpc

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for JsonRpc

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,