alloy_json_rpc/response/
payload.rs

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