pub struct RpcResponse {
pub jsonrpc: String,
pub id: Option<String>,
pub payload: RpcResponsePayload,
}Expand description
Response to a RpcRequest. Exactly one of result or error
is populated — see RpcResponsePayload.
Modelled as a struct with a flattened payload enum (rather than two field options) so the type system enforces the spec’s “exactly one of” requirement: it’s impossible to construct a response that has both, or neither.
id is Option<String> because JSON-RPC 2.0 mandates null
for errors that fire BEFORE the request id can be parsed —
super::error::ErrorKind::ParseError (the body wasn’t valid
JSON at all) and super::error::ErrorKind::InvalidRequest
(envelope rejected). RpcResponse::err_anonymous is the
dedicated constructor for that case; the happy-path Self::ok
/ Self::err keep the String ergonomic.
Fields§
§jsonrpc: String§id: Option<String>§payload: RpcResponsePayloadImplementations§
Source§impl RpcResponse
impl RpcResponse
Sourcepub fn ok<R: Serialize>(
id: impl Into<String>,
result: &R,
) -> Result<Self, Error>
pub fn ok<R: Serialize>( id: impl Into<String>, result: &R, ) -> Result<Self, Error>
Build a success response for a given request id from a
typed result. R = () is encoded as JSON null, matching
SPEC §2.12.7’s {"result":null} for void method returns.
Sourcepub fn err(id: impl Into<String>, error: RpcError) -> Self
pub fn err(id: impl Into<String>, error: RpcError) -> Self
Build an error response correlated to a known request id.
Sourcepub fn err_anonymous(error: RpcError) -> Self
pub fn err_anonymous(error: RpcError) -> Self
Build an error response with id: null — the JSON-RPC 2.0
shape for errors that fire before the request id can be
parsed (ParseError on un-decodable JSON; InvalidRequest
on an envelope missing required fields). Distinct from
Self::err so the type system makes “I don’t have an id
to correlate” an explicit choice.
Trait Implementations§
Source§impl Clone for RpcResponse
impl Clone for RpcResponse
Source§impl Debug for RpcResponse
impl Debug for RpcResponse
Source§impl<'de> Deserialize<'de> for RpcResponse
impl<'de> Deserialize<'de> for RpcResponse
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl JsonSchema for RpcResponse
impl JsonSchema for RpcResponse
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read more