Skip to content

Commit

Permalink
chore: add helper functions to ResponsePacket (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 15, 2024
1 parent 1443559 commit 19bed90
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
60 changes: 59 additions & 1 deletion crates/json-rpc/src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Id, Response, SerializedRequest};
use crate::{ErrorPayload, Id, Response, SerializedRequest};
use serde::{
de::{self, Deserializer, MapAccess, SeqAccess, Visitor},
Deserialize, Serialize,
Expand Down Expand Up @@ -217,6 +217,41 @@ impl BorrowedResponsePacket<'_> {
}

impl<Payload, ErrData> ResponsePacket<Payload, ErrData> {
/// Returns `true` if the response payload is a success.
///
/// For batch responses, this returns `true` if __all__ responses are successful.
pub fn is_success(&self) -> bool {
match self {
ResponsePacket::Single(single) => single.is_success(),
ResponsePacket::Batch(batch) => batch.iter().all(|res| res.is_success()),
}
}

/// Returns `true` if the response payload is a success.
///
/// For batch responses, this returns `true` there's at least one error response.
pub fn is_error(&self) -> bool {
match self {
ResponsePacket::Single(single) => single.is_error(),
ResponsePacket::Batch(batch) => batch.iter().any(|res| res.is_error()),
}
}

/// Returns the [ErrorPayload] if the response is an error.
///
/// For batch responses, this returns the first error response.
pub fn as_error(&self) -> Option<&ErrorPayload<ErrData>> {
self.iter_errors().next()
}

/// Returns an iterator over the [ErrorPayload]s in the response.
pub fn iter_errors(&self) -> impl Iterator<Item = &ErrorPayload<ErrData>> + '_ {
match self {
ResponsePacket::Single(single) => ResponsePacketErrorsIter::Single(Some(single)),
ResponsePacket::Batch(batch) => ResponsePacketErrorsIter::Batch(batch.iter()),
}
}

/// Find responses by a list of IDs.
///
/// This is intended to be used in conjunction with
Expand All @@ -241,3 +276,26 @@ impl<Payload, ErrData> ResponsePacket<Payload, ErrData> {
}
}
}

/// An Iterator over the [ErrorPayload]s in a [ResponsePacket].
#[derive(Debug, Clone)]
enum ResponsePacketErrorsIter<'a, Payload, ErrData> {
Single(Option<&'a Response<Payload, ErrData>>),
Batch(std::slice::Iter<'a, Response<Payload, ErrData>>),
}

impl<'a, Payload, ErrData> Iterator for ResponsePacketErrorsIter<'a, Payload, ErrData> {
type Item = &'a ErrorPayload<ErrData>;

fn next(&mut self) -> Option<Self::Item> {
match self {
ResponsePacketErrorsIter::Single(single) => single.take()?.payload.as_error(),
ResponsePacketErrorsIter::Batch(batch) => loop {
let res = batch.next()?;
if let Some(err) = res.payload.as_error() {
return Some(err);
}
},
}
}
}
2 changes: 1 addition & 1 deletion crates/json-rpc/src/response/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl BorrowedResponsePayload<'_> {
}

impl<Payload, ErrData> ResponsePayload<Payload, ErrData> {
/// Fallible conversion to the succesful payload.
/// Fallible conversion to the successful payload.
pub const fn as_success(&self) -> Option<&Payload> {
match self {
ResponsePayload::Success(payload) => Some(payload),
Expand Down

0 comments on commit 19bed90

Please sign in to comment.