Struct cuprate_json_rpc::Response

source ·
pub struct Response<T> {
    pub jsonrpc: Version,
    pub id: Id,
    pub payload: Result<T, ErrorObject>,
}
Expand description

The response object.

The generic T is the response payload, i.e. it is the type that holds both the method and params.

Fields§

§jsonrpc: Version

JSON-RPC protocol version; always 2.0.

§id: Id

This field must always be present in serialized JSON.

§JSON-RPC 2.0 rules
  • The Response’s ID must be the same as the Request
  • If the Request omitted the id field, there should be no Response
  • If there was an error in detecting the Request’s ID, the Response must contain an Id::Null
§payload: Result<T, ErrorObject>

The response payload.

§JSON-RPC 2.0 rules
  • This must be Ok upon success
  • This must be Err upon error
  • This can be any (de)serializable data T on success
  • This must be ErrorObject on errors

Implementations§

source§

impl<T> Response<T>

source

pub const fn ok(id: Id, result: T) -> Self

Creates a successful response.

use cuprate_json_rpc::{Id, Response};

let ok = Response::ok(Id::Num(123), "OK");
let json = serde_json::to_string(&ok).unwrap();
assert_eq!(json, r#"{"jsonrpc":"2.0","id":123,"result":"OK"}"#);
source

pub const fn err(id: Id, error: ErrorObject) -> Self

Creates an error response.

use cuprate_json_rpc::{Id, Response, error::{ErrorObject, ErrorCode}};

let err = ErrorObject {
    code: 0.into(),
    message: "m".into(),
    data: Some("d".into()),
};

let ok = Response::<()>::err(Id::Num(123), err);
let json = serde_json::to_string(&ok).unwrap();
assert_eq!(json, r#"{"jsonrpc":"2.0","id":123,"error":{"code":0,"message":"m","data":"d"}}"#);
source

pub const fn parse_error(id: Id) -> Self

Creates an error response using ErrorObject::parse_error.

use cuprate_json_rpc::{Id, Response, error::{ErrorObject, ErrorCode}};

let ok = Response::<()>::parse_error(Id::Num(0));
let json = serde_json::to_string(&ok).unwrap();
assert_eq!(json, r#"{"jsonrpc":"2.0","id":0,"error":{"code":-32700,"message":"Parse error"}}"#);
source

pub const fn invalid_request(id: Id) -> Self

Creates an error response using ErrorObject::invalid_request.

use cuprate_json_rpc::{Id, Response, error::{ErrorObject, ErrorCode}};

let ok = Response::<()>::invalid_request(Id::Num(0));
let json = serde_json::to_string(&ok).unwrap();
assert_eq!(json, r#"{"jsonrpc":"2.0","id":0,"error":{"code":-32600,"message":"Invalid Request"}}"#);
source

pub const fn method_not_found(id: Id) -> Self

Creates an error response using ErrorObject::method_not_found.

use cuprate_json_rpc::{Id, Response, error::{ErrorObject, ErrorCode}};

let ok = Response::<()>::method_not_found(Id::Num(0));
let json = serde_json::to_string(&ok).unwrap();
assert_eq!(json, r#"{"jsonrpc":"2.0","id":0,"error":{"code":-32601,"message":"Method not found"}}"#);
source

pub const fn invalid_params(id: Id) -> Self

Creates an error response using ErrorObject::invalid_params.

use cuprate_json_rpc::{Id, Response, error::{ErrorObject, ErrorCode}};

let ok = Response::<()>::invalid_params(Id::Num(0));
let json = serde_json::to_string(&ok).unwrap();
assert_eq!(json, r#"{"jsonrpc":"2.0","id":0,"error":{"code":-32602,"message":"Invalid params"}}"#);
source

pub const fn internal_error(id: Id) -> Self

Creates an error response using ErrorObject::internal_error.

use cuprate_json_rpc::{Id, Response, error::{ErrorObject, ErrorCode}};

let ok = Response::<()>::internal_error(Id::Num(0));
let json = serde_json::to_string(&ok).unwrap();
assert_eq!(json, r#"{"jsonrpc":"2.0","id":0,"error":{"code":-32603,"message":"Internal error"}}"#);

Trait Implementations§

source§

impl<T: Clone> Clone for Response<T>

source§

fn clone(&self) -> Response<T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Response<T>

source§

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

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

impl<'de, T> Deserialize<'de> for Response<T>
where T: Deserialize<'de> + 'de,

source§

fn deserialize<D: Deserializer<'de>>(der: D) -> Result<Self, D::Error>

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

impl<T> Display for Response<T>
where T: Serialize,

source§

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

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

impl<T: PartialEq> PartialEq for Response<T>

source§

fn eq(&self, other: &Response<T>) -> bool

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

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<T> Serialize for Response<T>
where T: Serialize,

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<T: Eq> Eq for Response<T>

source§

impl<T> StructuralPartialEq for Response<T>

Auto Trait Implementations§

§

impl<T> Freeze for Response<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Response<T>
where T: RefUnwindSafe,

§

impl<T> Send for Response<T>
where T: Send,

§

impl<T> Sync for Response<T>
where T: Sync,

§

impl<T> Unpin for Response<T>
where T: Unpin,

§

impl<T> UnwindSafe for Response<T>
where T: UnwindSafe,

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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>,

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.