pub struct Response<T> {
pub jsonrpc: Version,
pub id: Id,
pub payload: Result<T, ErrorObject>,
}
Expand description
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
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>
impl<T> Response<T>
Sourcepub const fn ok(id: Id, result: T) -> Self
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"}"#);
Sourcepub const fn err(id: Id, error: ErrorObject) -> Self
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"}}"#);
Sourcepub const fn parse_error(id: Id) -> Self
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"}}"#);
Sourcepub const fn invalid_request(id: Id) -> Self
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"}}"#);
Sourcepub const fn method_not_found(id: Id) -> Self
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"}}"#);
Sourcepub const fn invalid_params(id: Id) -> Self
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"}}"#);
Sourcepub const fn internal_error(id: Id) -> Self
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<'de, T> Deserialize<'de> for Response<T>where
T: Deserialize<'de> + 'de,
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>
fn deserialize<D: Deserializer<'de>>(der: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
impl<T: Eq> Eq for Response<T>
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> 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
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.