alloy_json_rpc/response/
payload.rs1use crate::{ErrorPayload, RpcSend};
2use serde::{de::DeserializeOwned, Deserialize};
3use serde_json::value::{to_raw_value, RawValue};
4use std::borrow::{Borrow, Cow};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
19pub enum ResponsePayload<Payload = Box<RawValue>, ErrData = Box<RawValue>> {
20 Success(Payload),
22 Failure(ErrorPayload<ErrData>),
24}
25
26pub type BorrowedResponsePayload<'a> = ResponsePayload<&'a RawValue, &'a RawValue>;
34
35impl BorrowedResponsePayload<'_> {
36 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 pub const fn parse_error() -> Self {
49 Self::Failure(ErrorPayload::parse_error())
50 }
51
52 pub const fn invalid_request() -> Self {
54 Self::Failure(ErrorPayload::invalid_request())
55 }
56
57 pub const fn method_not_found() -> Self {
59 Self::Failure(ErrorPayload::method_not_found())
60 }
61
62 pub const fn invalid_params() -> Self {
64 Self::Failure(ErrorPayload::invalid_params())
65 }
66
67 pub const fn internal_error() -> Self {
69 Self::Failure(ErrorPayload::internal_error())
70 }
71
72 pub const fn internal_error_message(message: Cow<'static, str>) -> Self {
74 Self::Failure(ErrorPayload::internal_error_message(message))
75 }
76
77 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 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 pub const fn as_success(&self) -> Option<&Payload> {
100 match self {
101 Self::Success(payload) => Some(payload),
102 _ => None,
103 }
104 }
105
106 pub const fn as_error(&self) -> Option<&ErrorPayload<ErrData>> {
108 match self {
109 Self::Failure(payload) => Some(payload),
110 _ => None,
111 }
112 }
113
114 pub const fn is_success(&self) -> bool {
116 matches!(self, Self::Success(_))
117 }
118
119 pub const fn is_error(&self) -> bool {
121 matches!(self, Self::Failure(_))
122 }
123}
124
125impl<Payload, ErrData> ResponsePayload<Payload, ErrData>
126where
127 Payload: RpcSend,
128 ErrData: RpcSend,
129{
130 pub fn serialize_payload(&self) -> serde_json::Result<ResponsePayload> {
132 match self {
133 Self::Success(payload) => Ok(ResponsePayload::Success(to_raw_value(payload)?)),
134 Self::Failure(error) => Ok(ResponsePayload::Failure(error.serialize_payload()?)),
135 }
136 }
137}
138
139impl<'a, Payload, ErrData> ResponsePayload<Payload, ErrData>
140where
141 Payload: AsRef<RawValue> + 'a,
142{
143 pub fn try_success_as<T: Deserialize<'a>>(&'a self) -> Option<serde_json::Result<T>> {
152 self.as_success().map(|payload| serde_json::from_str(payload.as_ref().get()))
153 }
154
155 pub fn deserialize_success<T: DeserializeOwned>(
163 self,
164 ) -> Result<ResponsePayload<T, ErrData>, Self> {
165 match self {
166 Self::Success(ref payload) => serde_json::from_str(payload.as_ref().get())
167 .map_or_else(|_| Err(self), |payload| Ok(ResponsePayload::Success(payload))),
168 Self::Failure(e) => Ok(ResponsePayload::Failure(e)),
169 }
170 }
171}
172
173impl<'a, Payload, Data> ResponsePayload<Payload, Data>
174where
175 Data: Borrow<RawValue> + 'a,
176{
177 pub fn try_error_as<T: Deserialize<'a>>(&'a self) -> Option<serde_json::Result<T>> {
185 self.as_error().and_then(|error| error.try_data_as::<T>())
186 }
187
188 pub fn deserialize_error<T: DeserializeOwned>(
196 self,
197 ) -> Result<ResponsePayload<Payload, T>, Self> {
198 match self {
199 Self::Failure(err) => match err.deser_data() {
200 Ok(deser) => Ok(ResponsePayload::Failure(deser)),
201 Err(err) => Err(Self::Failure(err)),
202 },
203 Self::Success(payload) => Ok(ResponsePayload::Success(payload)),
204 }
205 }
206}