alloy_json_rpc/response/payload.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
use crate::{ErrorPayload, RpcObject};
use serde::{de::DeserializeOwned, Deserialize};
use serde_json::value::{to_raw_value, RawValue};
use std::borrow::{Borrow, Cow};
/// A JSON-RPC 2.0 response payload.
///
/// This enum covers both the success and error cases of a JSON-RPC 2.0
/// response. It is used to represent the `result` and `error` fields of a
/// response object.
///
/// ### Note
///
/// This type does not implement `Serialize` or `Deserialize` directly. It is
/// deserialized as part of the [`Response`] type.
///
/// [`Response`]: crate::Response
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ResponsePayload<Payload = Box<RawValue>, ErrData = Box<RawValue>> {
/// A successful response payload.
Success(Payload),
/// An error response payload.
Failure(ErrorPayload<ErrData>),
}
/// A [`ResponsePayload`] that has been partially deserialized, borrowing its
/// contents from the deserializer. This is used primarily for intermediate
/// deserialization. Most users will not require it.
///
/// See the [top-level docs] for more info.
///
/// [top-level docs]: crate
pub type BorrowedResponsePayload<'a> = ResponsePayload<&'a RawValue, &'a RawValue>;
impl BorrowedResponsePayload<'_> {
/// Convert this borrowed response payload into an owned payload by copying
/// the data from the deserializer (if necessary).
pub fn into_owned(self) -> ResponsePayload {
match self {
Self::Success(payload) => ResponsePayload::Success(payload.to_owned()),
Self::Failure(error) => ResponsePayload::Failure(error.into_owned()),
}
}
}
impl<Payload, ErrData> ResponsePayload<Payload, ErrData> {
/// Create a new error payload for a parse error.
pub const fn parse_error() -> Self {
Self::Failure(ErrorPayload::parse_error())
}
/// Create a new error payload for an invalid request.
pub const fn invalid_request() -> Self {
Self::Failure(ErrorPayload::invalid_request())
}
/// Create a new error payload for a method not found error.
pub const fn method_not_found() -> Self {
Self::Failure(ErrorPayload::method_not_found())
}
/// Create a new error payload for an invalid params error.
pub const fn invalid_params() -> Self {
Self::Failure(ErrorPayload::invalid_params())
}
/// Create a new error payload for an internal error.
pub const fn internal_error() -> Self {
Self::Failure(ErrorPayload::internal_error())
}
/// Create a new error payload for an internal error with a custom message.
pub const fn internal_error_message(message: Cow<'static, str>) -> Self {
Self::Failure(ErrorPayload::internal_error_message(message))
}
/// Create a new error payload for an internal error with a custom message
/// and additional data.
pub const fn internal_error_with_obj(data: ErrData) -> Self
where
ErrData: RpcObject,
{
Self::Failure(ErrorPayload::internal_error_with_obj(data))
}
/// Create a new error payload for an internal error with a custom message
/// and additional data.
pub const fn internal_error_with_message_and_obj(
message: Cow<'static, str>,
data: ErrData,
) -> Self
where
ErrData: RpcObject,
{
Self::Failure(ErrorPayload::internal_error_with_message_and_obj(message, data))
}
/// Fallible conversion to the successful payload.
pub const fn as_success(&self) -> Option<&Payload> {
match self {
Self::Success(payload) => Some(payload),
_ => None,
}
}
/// Fallible conversion to the error object.
pub const fn as_error(&self) -> Option<&ErrorPayload<ErrData>> {
match self {
Self::Failure(payload) => Some(payload),
_ => None,
}
}
/// Returns `true` if the response payload is a success.
pub const fn is_success(&self) -> bool {
matches!(self, Self::Success(_))
}
/// Returns `true` if the response payload is an error.
pub const fn is_error(&self) -> bool {
matches!(self, Self::Failure(_))
}
}
impl<Payload, ErrData> ResponsePayload<Payload, ErrData>
where
Payload: RpcObject,
ErrData: RpcObject,
{
/// Convert the inner types into a [`RawValue`] by serializing them.
pub fn serialize_payload(&self) -> serde_json::Result<ResponsePayload> {
match self {
Self::Success(payload) => Ok(ResponsePayload::Success(to_raw_value(payload)?)),
Self::Failure(error) => Ok(ResponsePayload::Failure(error.serialize_payload()?)),
}
}
}
impl<'a, Payload, ErrData> ResponsePayload<Payload, ErrData>
where
Payload: AsRef<RawValue> + 'a,
{
/// Attempt to deserialize the success payload, borrowing from the payload
/// if necessary.
///
/// # Returns
/// - `None` if the payload is an error
/// - `Some(Ok(T))` if the payload is a success and can be deserialized
/// - `Some(Err(serde_json::Error))` if the payload is a success and can't be deserialized as
/// `T`
pub fn try_success_as<T: Deserialize<'a>>(&'a self) -> Option<serde_json::Result<T>> {
self.as_success().map(|payload| serde_json::from_str(payload.as_ref().get()))
}
/// Deserialize a Success payload, if possible, transforming this type.
///
/// # Returns
///
/// - `Ok(ResponsePayload<T>)` if the payload is an error, or if the payload is a success and
/// can be deserialized as `T`
/// - `Err(self)` if the payload is a success and can't be deserialized
pub fn deserialize_success<T: DeserializeOwned>(
self,
) -> Result<ResponsePayload<T, ErrData>, Self> {
match self {
Self::Success(ref payload) => serde_json::from_str(payload.as_ref().get())
.map_or_else(|_| Err(self), |payload| Ok(ResponsePayload::Success(payload))),
Self::Failure(e) => Ok(ResponsePayload::Failure(e)),
}
}
}
impl<'a, Payload, Data> ResponsePayload<Payload, Data>
where
Data: Borrow<RawValue> + 'a,
{
/// Attempt to deserialize the error payload, borrowing from the payload if
/// necessary.
///
/// # Returns
/// - `None` if the payload is a success
/// - `Some(Ok(T))` if the payload is an error and can be deserialized
/// - `Some(Err(serde_json::Error))` if the payload is an error and can't be deserialized as `T`
pub fn try_error_as<T: Deserialize<'a>>(&'a self) -> Option<serde_json::Result<T>> {
self.as_error().and_then(|error| error.try_data_as::<T>())
}
/// Deserialize an Error payload, if possible, transforming this type.
///
/// # Returns
///
/// - `Ok(ResponsePayload<Payload, T>)` if the payload is an error, or if the payload is an
/// error and can be deserialized as `T`.
/// - `Err(self)` if the payload is an error and can't be deserialized.
pub fn deserialize_error<T: DeserializeOwned>(
self,
) -> Result<ResponsePayload<Payload, T>, Self> {
match self {
Self::Failure(err) => match err.deser_data() {
Ok(deser) => Ok(ResponsePayload::Failure(deser)),
Err(err) => Err(Self::Failure(err)),
},
Self::Success(payload) => Ok(ResponsePayload::Success(payload)),
}
}
}