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 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    pub fn error_code(&self) -> Option<i64> {
124        self.as_error().map(|err| err.code)
125    }
126
127    pub fn error_data(&self) -> Option<&ErrData> {
129        self.as_error().and_then(|err| err.data.as_ref())
130    }
131
132    pub const fn is_success(&self) -> bool {
134        matches!(self, Self::Success(_))
135    }
136
137    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    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    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    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    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    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}