alloy_json_rpc/
packet.rs

1use crate::{ErrorPayload, Id, Response, ResponsePayload, SerializedRequest};
2use alloy_primitives::map::HashSet;
3use http::HeaderMap;
4use serde::{
5    de::{self, Deserializer, MapAccess, SeqAccess, Visitor},
6    Deserialize, Serialize,
7};
8use serde_json::value::RawValue;
9use std::{fmt, marker::PhantomData};
10
11/// A [`RequestPacket`] is a [`SerializedRequest`] or a batch of serialized
12/// request.
13#[derive(Clone, Debug)]
14pub enum RequestPacket {
15    /// A single request.
16    Single(SerializedRequest),
17    /// A batch of requests.
18    Batch(Vec<SerializedRequest>),
19}
20
21impl FromIterator<SerializedRequest> for RequestPacket {
22    fn from_iter<T: IntoIterator<Item = SerializedRequest>>(iter: T) -> Self {
23        Self::Batch(iter.into_iter().collect())
24    }
25}
26
27impl From<SerializedRequest> for RequestPacket {
28    fn from(req: SerializedRequest) -> Self {
29        Self::Single(req)
30    }
31}
32
33impl Serialize for RequestPacket {
34    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
35    where
36        S: serde::Serializer,
37    {
38        match self {
39            Self::Single(single) => single.serialize(serializer),
40            Self::Batch(batch) => batch.serialize(serializer),
41        }
42    }
43}
44
45impl RequestPacket {
46    /// Create a new empty packet with the given capacity.
47    pub fn with_capacity(capacity: usize) -> Self {
48        Self::Batch(Vec::with_capacity(capacity))
49    }
50
51    /// Returns the [`SerializedRequest`] if this packet is [`ResponsePacket::Single`]
52    pub const fn as_single(&self) -> Option<&SerializedRequest> {
53        match self {
54            Self::Single(req) => Some(req),
55            Self::Batch(_) => None,
56        }
57    }
58
59    /// Returns the batch of [`SerializedRequest`] if this packet is [`ResponsePacket::Batch`]
60    pub fn as_batch(&self) -> Option<&[SerializedRequest]> {
61        match self {
62            Self::Batch(req) => Some(req.as_slice()),
63            Self::Single(_) => None,
64        }
65    }
66
67    /// Serialize the packet as a boxed [`RawValue`].
68    pub fn serialize(self) -> serde_json::Result<Box<RawValue>> {
69        match self {
70            Self::Single(single) => Ok(single.take_request()),
71            Self::Batch(batch) => serde_json::value::to_raw_value(&batch),
72        }
73    }
74
75    /// Get the request IDs of all subscription requests in the packet.
76    pub fn subscription_request_ids(&self) -> HashSet<&Id> {
77        match self {
78            Self::Single(single) => {
79                let id = (single.method() == "eth_subscribe").then(|| single.id());
80                HashSet::from_iter(id)
81            }
82            Self::Batch(batch) => batch
83                .iter()
84                .filter(|req| req.method() == "eth_subscribe")
85                .map(|req| req.id())
86                .collect(),
87        }
88    }
89
90    /// Get the number of requests in the packet.
91    pub fn len(&self) -> usize {
92        match self {
93            Self::Single(_) => 1,
94            Self::Batch(batch) => batch.len(),
95        }
96    }
97
98    /// Check if the packet is empty.
99    pub fn is_empty(&self) -> bool {
100        self.len() == 0
101    }
102
103    /// Push a request into the packet.
104    pub fn push(&mut self, req: SerializedRequest) {
105        match self {
106            Self::Batch(batch) => batch.push(req),
107            Self::Single(_) => {
108                let old = std::mem::replace(self, Self::Batch(Vec::with_capacity(10)));
109                if let Self::Single(single) = old {
110                    self.push(single);
111                }
112                self.push(req);
113            }
114        }
115    }
116
117    /// Returns a all [`SerializedRequest`].
118    pub fn requests(&self) -> &[SerializedRequest] {
119        match self {
120            Self::Single(req) => std::slice::from_ref(req),
121            Self::Batch(req) => req.as_slice(),
122        }
123    }
124
125    /// Returns an iterator over the requests' method names
126    pub fn method_names(&self) -> impl Iterator<Item = &str> + '_ {
127        self.requests().iter().map(|req| req.method())
128    }
129
130    /// Retrieves the [`HeaderMap`] from the request metadata if available;
131    /// otherwise, returns an empty map. This functionality is only supported for single requests.
132    pub fn headers(&self) -> HeaderMap {
133        // If this is a batch request, we cannot return headers.
134        let Some(single_req) = self.as_single() else {
135            return HeaderMap::new();
136        };
137        // If the request provides a `HeaderMap` return it.
138        if let Some(http_header_extension) = single_req.meta().extensions().get::<HeaderMap>() {
139            return http_header_extension.clone();
140        };
141
142        HeaderMap::new()
143    }
144}
145
146/// A [`ResponsePacket`] is a [`Response`] or a batch of responses.
147#[derive(Clone, Debug)]
148pub enum ResponsePacket<Payload = Box<RawValue>, ErrData = Box<RawValue>> {
149    /// A single response.
150    Single(Response<Payload, ErrData>),
151    /// A batch of responses.
152    Batch(Vec<Response<Payload, ErrData>>),
153}
154
155impl<Payload, ErrData> FromIterator<Response<Payload, ErrData>>
156    for ResponsePacket<Payload, ErrData>
157{
158    fn from_iter<T: IntoIterator<Item = Response<Payload, ErrData>>>(iter: T) -> Self {
159        let mut iter = iter.into_iter().peekable();
160        // return single if iter has exactly one element, else make a batch
161        if let Some(first) = iter.next() {
162            return if iter.peek().is_none() {
163                Self::Single(first)
164            } else {
165                let mut batch = Vec::new();
166                batch.push(first);
167                batch.extend(iter);
168                Self::Batch(batch)
169            };
170        }
171        Self::Batch(vec![])
172    }
173}
174
175impl<Payload, ErrData> From<Vec<Response<Payload, ErrData>>> for ResponsePacket<Payload, ErrData> {
176    fn from(value: Vec<Response<Payload, ErrData>>) -> Self {
177        if value.len() == 1 {
178            Self::Single(value.into_iter().next().unwrap())
179        } else {
180            Self::Batch(value)
181        }
182    }
183}
184
185impl<'de, Payload, ErrData> Deserialize<'de> for ResponsePacket<Payload, ErrData>
186where
187    Payload: Deserialize<'de>,
188    ErrData: Deserialize<'de>,
189{
190    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
191    where
192        D: Deserializer<'de>,
193    {
194        struct ResponsePacketVisitor<Payload, ErrData> {
195            marker: PhantomData<fn() -> ResponsePacket<Payload, ErrData>>,
196        }
197
198        impl<'de, Payload, ErrData> Visitor<'de> for ResponsePacketVisitor<Payload, ErrData>
199        where
200            Payload: Deserialize<'de>,
201            ErrData: Deserialize<'de>,
202        {
203            type Value = ResponsePacket<Payload, ErrData>;
204
205            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
206                formatter.write_str("a single response or a batch of responses")
207            }
208
209            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
210            where
211                A: SeqAccess<'de>,
212            {
213                let mut responses = Vec::new();
214
215                while let Some(response) = seq.next_element()? {
216                    responses.push(response);
217                }
218
219                Ok(ResponsePacket::Batch(responses))
220            }
221
222            fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
223            where
224                M: MapAccess<'de>,
225            {
226                let response =
227                    Deserialize::deserialize(de::value::MapAccessDeserializer::new(map))?;
228                Ok(ResponsePacket::Single(response))
229            }
230        }
231
232        deserializer.deserialize_any(ResponsePacketVisitor { marker: PhantomData })
233    }
234}
235
236/// A [`BorrowedResponsePacket`] is a [`ResponsePacket`] that has been partially deserialized,
237/// borrowing its contents from the deserializer.
238///
239/// This is used primarily for intermediate deserialization. Most users will not require it.
240///
241/// See the [top-level docs] for more info.
242///
243/// [top-level docs]: crate
244pub type BorrowedResponsePacket<'a> = ResponsePacket<&'a RawValue, &'a RawValue>;
245
246impl BorrowedResponsePacket<'_> {
247    /// Convert this borrowed response packet into an owned packet by copying
248    /// the data from the deserializer (if necessary).
249    pub fn into_owned(self) -> ResponsePacket {
250        match self {
251            Self::Single(single) => ResponsePacket::Single(single.into_owned()),
252            Self::Batch(batch) => {
253                ResponsePacket::Batch(batch.into_iter().map(Response::into_owned).collect())
254            }
255        }
256    }
257}
258
259impl<Payload, ErrData> ResponsePacket<Payload, ErrData> {
260    /// Returns the [`Response`] if this packet is [`ResponsePacket::Single`].
261    pub const fn as_single(&self) -> Option<&Response<Payload, ErrData>> {
262        match self {
263            Self::Single(resp) => Some(resp),
264            Self::Batch(_) => None,
265        }
266    }
267
268    /// Returns the batch of [`Response`] if this packet is [`ResponsePacket::Batch`].
269    pub fn as_batch(&self) -> Option<&[Response<Payload, ErrData>]> {
270        match self {
271            Self::Batch(resp) => Some(resp.as_slice()),
272            Self::Single(_) => None,
273        }
274    }
275
276    /// Returns the [`ResponsePayload`] if this packet is [`ResponsePacket::Single`].
277    pub fn single_payload(&self) -> Option<&ResponsePayload<Payload, ErrData>> {
278        self.as_single().map(|resp| &resp.payload)
279    }
280
281    /// Returns `true` if the response payload is a success.
282    ///
283    /// For batch responses, this returns `true` if __all__ responses are successful.
284    pub fn is_success(&self) -> bool {
285        match self {
286            Self::Single(single) => single.is_success(),
287            Self::Batch(batch) => batch.iter().all(|res| res.is_success()),
288        }
289    }
290
291    /// Returns `true` if the response payload is an error.
292    ///
293    /// For batch responses, this returns `true` there's at least one error response.
294    pub fn is_error(&self) -> bool {
295        match self {
296            Self::Single(single) => single.is_error(),
297            Self::Batch(batch) => batch.iter().any(|res| res.is_error()),
298        }
299    }
300
301    /// Returns the [ErrorPayload] if the response is an error.
302    ///
303    /// For batch responses, this returns the first error response.
304    pub fn as_error(&self) -> Option<&ErrorPayload<ErrData>> {
305        self.iter_errors().next()
306    }
307
308    /// Returns an iterator over the [ErrorPayload]s in the response.
309    pub fn iter_errors(&self) -> impl Iterator<Item = &ErrorPayload<ErrData>> + '_ {
310        match self {
311            Self::Single(single) => ResponsePacketErrorsIter::Single(Some(single)),
312            Self::Batch(batch) => ResponsePacketErrorsIter::Batch(batch.iter()),
313        }
314    }
315
316    /// Returns the first error code in this packet if it contains any error responses.
317    pub fn first_error_code(&self) -> Option<i64> {
318        self.as_error().map(|error| error.code)
319    }
320
321    /// Returns the first error message in this packet if it contains any error responses.
322    pub fn first_error_message(&self) -> Option<&str> {
323        self.as_error().map(|error| error.message.as_ref())
324    }
325
326    /// Returns the first error data in this packet if it contains any error responses.
327    pub fn first_error_data(&self) -> Option<&ErrData> {
328        self.as_error().and_then(|error| error.data.as_ref())
329    }
330
331    /// Returns a all [`Response`].
332    pub fn responses(&self) -> &[Response<Payload, ErrData>] {
333        match self {
334            Self::Single(req) => std::slice::from_ref(req),
335            Self::Batch(req) => req.as_slice(),
336        }
337    }
338
339    /// Returns an iterator over the responses' payloads.
340    pub fn payloads(&self) -> impl Iterator<Item = &ResponsePayload<Payload, ErrData>> + '_ {
341        self.responses().iter().map(|resp| &resp.payload)
342    }
343
344    /// Returns the first [`ResponsePayload`] in this packet.
345    pub fn first_payload(&self) -> Option<&ResponsePayload<Payload, ErrData>> {
346        self.payloads().next()
347    }
348
349    /// Returns an iterator over the responses' identifiers.
350    pub fn response_ids(&self) -> impl Iterator<Item = &Id> + '_ {
351        self.responses().iter().map(|resp| &resp.id)
352    }
353
354    /// Find responses by a list of IDs.
355    ///
356    /// This is intended to be used in conjunction with
357    /// [`RequestPacket::subscription_request_ids`] to identify subscription
358    /// responses.
359    ///
360    /// # Note
361    ///
362    /// - Responses are not guaranteed to be in the same order.
363    /// - Responses are not guaranteed to be in the set.
364    /// - If the packet contains duplicate IDs, both will be found.
365    pub fn responses_by_ids(&self, ids: &HashSet<Id>) -> Vec<&Response<Payload, ErrData>> {
366        match self {
367            Self::Single(single) if ids.contains(&single.id) => vec![single],
368            Self::Batch(batch) => batch.iter().filter(|res| ids.contains(&res.id)).collect(),
369            _ => Vec::new(),
370        }
371    }
372}
373
374/// An Iterator over the [ErrorPayload]s in a [ResponsePacket].
375#[derive(Clone, Debug)]
376enum ResponsePacketErrorsIter<'a, Payload, ErrData> {
377    Single(Option<&'a Response<Payload, ErrData>>),
378    Batch(std::slice::Iter<'a, Response<Payload, ErrData>>),
379}
380
381impl<'a, Payload, ErrData> Iterator for ResponsePacketErrorsIter<'a, Payload, ErrData> {
382    type Item = &'a ErrorPayload<ErrData>;
383
384    fn next(&mut self) -> Option<Self::Item> {
385        match self {
386            ResponsePacketErrorsIter::Single(single) => single.take()?.payload.as_error(),
387            ResponsePacketErrorsIter::Batch(batch) => loop {
388                let res = batch.next()?;
389                if let Some(err) = res.payload.as_error() {
390                    return Some(err);
391                }
392            },
393        }
394    }
395}