diff --git a/watermelon/src/client/commands/publish.rs b/watermelon/src/client/commands/publish.rs index aead137..7b807a1 100644 --- a/watermelon/src/client/commands/publish.rs +++ b/watermelon/src/client/commands/publish.rs @@ -4,6 +4,7 @@ use std::{ }; use bytes::Bytes; +use serde::Serialize; use watermelon_proto::{ MessageBase, Subject, headers::{HeaderMap, HeaderName, HeaderValue}, @@ -65,7 +66,7 @@ pub struct DoOwnedClientPublish { } macro_rules! publish { - () => { + ($payload_t:ty) => { #[must_use] pub fn reply_subject(mut self, reply_subject: Option) -> Self { self.publish_mut().reply_subject = reply_subject; @@ -83,6 +84,19 @@ macro_rules! publish { self.publish_mut().headers = headers; self } + + /// Serialize `payload` to JSON and use it as the payload + /// + /// # Errors + /// + /// Returns an error if the serializer fails + pub fn payload_json( + self, + payload: &T, + ) -> Result<$payload_t, serde_json::Error> { + let payload = serde_json::to_vec(payload)?; + Ok(self.payload(payload.into())) + } }; } @@ -145,7 +159,7 @@ impl PublishBuilder { } } - publish!(); + publish!(Publish); #[must_use] pub fn payload(mut self, payload: Bytes) -> Publish { @@ -166,7 +180,7 @@ impl<'a> ClientPublish<'a> { } } - publish!(); + publish!(DoClientPublish<'a>); pub fn payload(mut self, payload: Bytes) -> DoClientPublish<'a> { self.publish.payload = payload; @@ -195,7 +209,7 @@ impl OwnedClientPublish { } } - publish!(); + publish!(DoOwnedClientPublish); pub fn payload(mut self, payload: Bytes) -> DoOwnedClientPublish { self.publish.payload = payload; diff --git a/watermelon/src/client/commands/request.rs b/watermelon/src/client/commands/request.rs index 665365a..28e2ec0 100644 --- a/watermelon/src/client/commands/request.rs +++ b/watermelon/src/client/commands/request.rs @@ -10,6 +10,7 @@ use std::{ use bytes::Bytes; use futures_core::Stream; use pin_project_lite::pin_project; +use serde::Serialize; use tokio::time::{Sleep, sleep}; use watermelon_proto::{ ServerMessage, StatusCode, Subject, @@ -109,7 +110,7 @@ pub enum ResponseError { } macro_rules! request { - () => { + ($payload_t:ty) => { #[must_use] pub fn reply_subject(mut self, reply_subject: Option) -> Self { self.request_mut().publish.reply_subject = reply_subject; @@ -133,6 +134,19 @@ macro_rules! request { self.request_mut().response_timeout = Some(timeout); self } + + /// Serialize `payload` to JSON and use it as the payload + /// + /// # Errors + /// + /// Returns an error if the serializer fails + pub fn payload_json( + self, + payload: &T, + ) -> Result<$payload_t, serde_json::Error> { + let payload = serde_json::to_vec(payload)?; + Ok(self.payload(payload.into())) + } }; } @@ -176,7 +190,7 @@ impl RequestBuilder { } } - request!(); + request!(Request); #[must_use] pub fn payload(mut self, payload: Bytes) -> Request { @@ -197,7 +211,7 @@ impl<'a> ClientRequest<'a> { } } - request!(); + request!(DoClientRequest<'a>); pub fn payload(mut self, payload: Bytes) -> DoClientRequest<'a> { self.request.publish.payload = payload; @@ -226,7 +240,7 @@ impl OwnedClientRequest { } } - request!(); + request!(DoOwnedClientRequest); pub fn payload(mut self, payload: Bytes) -> DoOwnedClientRequest { self.request.publish.payload = payload;