Skip to content

Commit a3da373

Browse files
authored
feat: add helpers for rpc types (#2300)
1 parent 0b523fb commit a3da373

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

crates/json-rpc/src/packet.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ErrorPayload, Id, Response, SerializedRequest};
1+
use crate::{ErrorPayload, Id, Response, ResponsePayload, SerializedRequest};
22
use alloy_primitives::map::HashSet;
33
use serde::{
44
de::{self, Deserializer, MapAccess, SeqAccess, Visitor},
@@ -96,6 +96,19 @@ impl RequestPacket {
9696
}
9797
}
9898
}
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+
}
99112
}
100113

101114
/// A [`ResponsePacket`] is a [`Response`] or a batch of responses.
@@ -247,6 +260,24 @@ impl<Payload, ErrData> ResponsePacket<Payload, ErrData> {
247260
}
248261
}
249262

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+
250281
/// Find responses by a list of IDs.
251282
///
252283
/// This is intended to be used in conjunction with

0 commit comments

Comments
 (0)