|
1 | | -use crate::{ErrorPayload, Id, Response, SerializedRequest}; |
| 1 | +use crate::{ErrorPayload, Id, Response, ResponsePayload, SerializedRequest}; |
2 | 2 | use alloy_primitives::map::HashSet; |
3 | 3 | use serde::{ |
4 | 4 | de::{self, Deserializer, MapAccess, SeqAccess, Visitor}, |
@@ -96,6 +96,19 @@ impl RequestPacket { |
96 | 96 | } |
97 | 97 | } |
98 | 98 | } |
| 99 | + |
| 100 | + /// Returns a all [`SerializedRequest`]. |
| 101 | + pub fn requests(&self) -> &[SerializedRequest] { |
| 102 | + match self { |
| 103 | + Self::Single(req) => std::slice::from_ref(req), |
| 104 | + Self::Batch(req) => req.as_slice(), |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /// Returns an iterator over the requests' method names |
| 109 | + pub fn method_names(&self) -> impl Iterator<Item = &str> + '_ { |
| 110 | + self.requests().iter().map(|req| req.method()) |
| 111 | + } |
99 | 112 | } |
100 | 113 |
|
101 | 114 | /// A [`ResponsePacket`] is a [`Response`] or a batch of responses. |
@@ -247,6 +260,24 @@ impl<Payload, ErrData> ResponsePacket<Payload, ErrData> { |
247 | 260 | } |
248 | 261 | } |
249 | 262 |
|
| 263 | + /// Returns a all [`Response`]. |
| 264 | + pub fn responses(&self) -> &[Response<Payload, ErrData>] { |
| 265 | + match self { |
| 266 | + Self::Single(req) => std::slice::from_ref(req), |
| 267 | + Self::Batch(req) => req.as_slice(), |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + /// Returns an iterator over the responses' payloads. |
| 272 | + pub fn payloads(&self) -> impl Iterator<Item = &ResponsePayload<Payload, ErrData>> + '_ { |
| 273 | + self.responses().iter().map(|resp| &resp.payload) |
| 274 | + } |
| 275 | + |
| 276 | + /// Returns an iterator over the responses' identifiers. |
| 277 | + pub fn response_ids(&self) -> impl Iterator<Item = &Id> + '_ { |
| 278 | + self.responses().iter().map(|resp| &resp.id) |
| 279 | + } |
| 280 | + |
250 | 281 | /// Find responses by a list of IDs. |
251 | 282 | /// |
252 | 283 | /// This is intended to be used in conjunction with |
|
0 commit comments