Skip to content

Commit

Permalink
chore(pickup-messages): custom decorator struct per message type
Browse files Browse the repository at this point in the history
Signed-off-by: Naian <126972030+nain-F49FF806@users.noreply.github.com>
  • Loading branch information
nain-F49FF806 committed Oct 18, 2023
1 parent fc9a27c commit a9c5b2f
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 57 deletions.
1 change: 1 addition & 0 deletions messages/src/decorators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod localization;
pub mod please_ack;
pub mod thread;
pub mod timing;
pub mod transport;
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ use typed_builder::TypedBuilder;

use crate::decorators::thread::Thread;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct PickupDecoratorsCommon {
#[builder(default, setter(strip_option))]
#[serde(rename = "~thread")]
#[serde(skip_serializing_if = "Option::is_none")]
pub thread: Option<Thread>,
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct Transport {
pub return_route: ReturnRoute,
Expand Down Expand Up @@ -44,16 +32,32 @@ mod tests {
use crate::misc::test_utils;

#[test]
fn test_transport_decorator() {
fn test_transport_minimals() {
// all variant
let transport = Transport::builder().return_route(ReturnRoute::All).build();
let decorators = PickupDecoratorsCommon::builder()
.transport(transport)
.build();
let expected = json!({
"~transport": {
"return_route": "all"
}
});
test_utils::test_serde(decorators, expected);
test_utils::test_serde(transport, expected);
// none variant
let transport = Transport::builder().return_route(ReturnRoute::None).build();
let expected = json!({
"return_route": "none"
});
test_utils::test_serde(transport, expected);
}
#[test]
fn test_transport_extended() {
// thread variant
let thread = Thread::builder().thid("<thread id>".to_string()).build();
let transport = Transport::builder()
.return_route(ReturnRoute::Thread)
.return_route_thread(thread)
.build();
let expected = json!({
"return_route": "thread",
"return_route_thread": { "thid": "<thread id>" }
});
test_utils::test_serde(transport, expected);
}
}
22 changes: 18 additions & 4 deletions messages/src/msg_fields/protocols/pickup/delivery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use serde::{Deserialize, Serialize};
use serde_with::{base64::Base64, serde_as};
use typed_builder::TypedBuilder;

use super::decorators::PickupDecoratorsCommon;
use crate::msg_parts::MsgParts;
use crate::{
decorators::{thread::Thread, transport::Transport},
msg_parts::MsgParts,
};

pub type Delivery = MsgParts<DeliveryContent, PickupDecoratorsCommon>;
pub type Delivery = MsgParts<DeliveryContent, DeliveryDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryContent {
Expand All @@ -30,6 +32,18 @@ pub struct DeliveryAttachData {
pub base64: Vec<u8>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
#[builder(default, setter(strip_option))]
#[serde(rename = "~thread")]
#[serde(skip_serializing_if = "Option::is_none")]
pub thread: Option<Thread>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
Expand Down Expand Up @@ -66,7 +80,7 @@ mod tests {
.recipient_key("<key for messages>".to_owned())
.attach(vec![attach])
.build();
let decorators = PickupDecoratorsCommon::builder()
let decorators = DeliveryDecorators::builder()
.thread(
Thread::builder()
.thid("<message id of delivery-request message>".to_owned())
Expand Down
22 changes: 18 additions & 4 deletions messages/src/msg_fields/protocols/pickup/delivery_request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::decorators::PickupDecoratorsCommon;
use crate::msg_parts::MsgParts;
use crate::{
decorators::{thread::Thread, transport::Transport},
msg_parts::MsgParts,
};

pub type DeliveryRequest = MsgParts<DeliveryRequestContent, PickupDecoratorsCommon>;
pub type DeliveryRequest = MsgParts<DeliveryRequestContent, DeliveryRequestDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryRequestContent {
Expand All @@ -14,6 +16,18 @@ pub struct DeliveryRequestContent {
pub recipient_key: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct DeliveryRequestDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~thread")]
#[serde(skip_serializing_if = "Option::is_none")]
pub thread: Option<Thread>,
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
Expand All @@ -36,7 +50,7 @@ mod tests {
.recipient_key("<key for messages>".to_owned())
.limit(10)
.build();
let decorators = PickupDecoratorsCommon::builder().build();
let decorators = DeliveryRequestDecorators::builder().build();

test_utils::test_msg(
content,
Expand Down
15 changes: 11 additions & 4 deletions messages/src/msg_fields/protocols/pickup/live_delivery_change.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::decorators::PickupDecoratorsCommon;
use crate::msg_parts::MsgParts;
use crate::{decorators::transport::Transport, msg_parts::MsgParts};

pub type LiveDeliveryChange = MsgParts<LiveDeliveryChangeContent, PickupDecoratorsCommon>;
pub type LiveDeliveryChange = MsgParts<LiveDeliveryChangeContent, LiveDeliveryChangeDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct LiveDeliveryChangeContent {
pub live_delivery: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct LiveDeliveryChangeDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
Expand All @@ -30,7 +37,7 @@ mod tests {
let content = LiveDeliveryChangeContent::builder()
.live_delivery(true)
.build();
let decorators = PickupDecoratorsCommon::builder().build();
let decorators = LiveDeliveryChangeDecorators::builder().build();

test_utils::test_msg(
content,
Expand Down
15 changes: 11 additions & 4 deletions messages/src/msg_fields/protocols/pickup/messages_received.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::decorators::PickupDecoratorsCommon;
use crate::msg_parts::MsgParts;
use crate::{decorators::transport::Transport, msg_parts::MsgParts};

pub type MessagesReceived = MsgParts<MessagesReceivedContent, PickupDecoratorsCommon>;
pub type MessagesReceived = MsgParts<MessagesReceivedContent, MessagesReceivedDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct MessagesReceivedContent {
pub message_id_list: Vec<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct MessagesReceivedDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
Expand All @@ -31,7 +38,7 @@ mod tests {
let content = MessagesReceivedContent::builder()
.message_id_list(vec!["123".to_string(), "456".to_string()])
.build();
let decorators = PickupDecoratorsCommon::builder().build();
let decorators = MessagesReceivedDecorators::builder().build();

test_utils::test_msg(
content,
Expand Down
28 changes: 14 additions & 14 deletions messages/src/msg_fields/protocols/pickup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod decorators;
mod delivery;
mod delivery_request;
mod live_delivery_change;
Expand All @@ -9,13 +8,14 @@ use derive_more::From;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};

use self::{
decorators::PickupDecoratorsCommon,
delivery::{Delivery, DeliveryContent},
delivery_request::{DeliveryRequest, DeliveryRequestContent},
live_delivery_change::{LiveDeliveryChange, LiveDeliveryChangeContent},
messages_received::{MessagesReceived, MessagesReceivedContent},
status::{Status, StatusContent},
status_request::{StatusRequest, StatusRequestContent},
delivery::{Delivery, DeliveryContent, DeliveryDecorators},
delivery_request::{DeliveryRequest, DeliveryRequestContent, DeliveryRequestDecorators},
live_delivery_change::{
LiveDeliveryChange, LiveDeliveryChangeContent, LiveDeliveryChangeDecorators,
},
messages_received::{MessagesReceived, MessagesReceivedContent, MessagesReceivedDecorators},
status::{Status, StatusContent, StatusDecorators},
status_request::{StatusRequest, StatusRequestContent, StatusRequestDecorators},
};
use crate::{
misc::utils::{into_msg_with_type, transit_to_aries_msg},
Expand Down Expand Up @@ -85,12 +85,12 @@ impl DelayedSerde for Pickup {
}
}

transit_to_aries_msg!(StatusContent: PickupDecoratorsCommon, Pickup);
transit_to_aries_msg!(StatusRequestContent: PickupDecoratorsCommon, Pickup);
transit_to_aries_msg!(DeliveryContent: PickupDecoratorsCommon, Pickup);
transit_to_aries_msg!(DeliveryRequestContent: PickupDecoratorsCommon, Pickup);
transit_to_aries_msg!(MessagesReceivedContent: PickupDecoratorsCommon, Pickup);
transit_to_aries_msg!(LiveDeliveryChangeContent: PickupDecoratorsCommon, Pickup);
transit_to_aries_msg!(StatusContent: StatusDecorators, Pickup);
transit_to_aries_msg!(StatusRequestContent: StatusRequestDecorators, Pickup);
transit_to_aries_msg!(DeliveryContent: DeliveryDecorators, Pickup);
transit_to_aries_msg!(DeliveryRequestContent: DeliveryRequestDecorators, Pickup);
transit_to_aries_msg!(MessagesReceivedContent: MessagesReceivedDecorators, Pickup);
transit_to_aries_msg!(LiveDeliveryChangeContent: LiveDeliveryChangeDecorators, Pickup);

into_msg_with_type!(Status, PickupTypeV2_0, Status);
into_msg_with_type!(StatusRequest, PickupTypeV2_0, StatusRequest);
Expand Down
15 changes: 11 additions & 4 deletions messages/src/msg_fields/protocols/pickup/status.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::decorators::PickupDecoratorsCommon;
use crate::msg_parts::MsgParts;
use crate::{decorators::transport::Transport, msg_parts::MsgParts};

pub type Status = MsgParts<StatusContent, PickupDecoratorsCommon>;
pub type Status = MsgParts<StatusContent, StatusDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct StatusContent {
Expand All @@ -14,6 +13,14 @@ pub struct StatusContent {
pub recipient_key: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct StatusDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
Expand All @@ -37,7 +44,7 @@ mod tests {
.recipient_key("<key for messages>".to_owned())
.message_count(7)
.build();
let decorators = PickupDecoratorsCommon::builder().build();
let decorators = StatusDecorators::builder().build();

test_utils::test_msg(content, decorators, PickupTypeV2_0::Status, expected);
}
Expand Down
15 changes: 11 additions & 4 deletions messages/src/msg_fields/protocols/pickup/status_request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::decorators::PickupDecoratorsCommon;
use crate::msg_parts::MsgParts;
use crate::{decorators::transport::Transport, msg_parts::MsgParts};

pub type StatusRequest = MsgParts<StatusRequestContent, PickupDecoratorsCommon>;
pub type StatusRequest = MsgParts<StatusRequestContent, StatusRequestDecorators>;

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct StatusRequestContent {
Expand All @@ -13,6 +12,14 @@ pub struct StatusRequestContent {
pub recipient_key: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]
pub struct StatusRequestDecorators {
#[builder(default, setter(strip_option))]
#[serde(rename = "~transport")]
#[serde(skip_serializing_if = "Option::is_none")]
pub transport: Option<Transport>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
Expand All @@ -34,7 +41,7 @@ mod tests {
let content = StatusRequestContent::builder()
.recipient_key("<key for messages>".to_owned())
.build();
let decorators = PickupDecoratorsCommon::builder().build();
let decorators = StatusRequestDecorators::builder().build();

test_utils::test_msg(content, decorators, PickupTypeV2_0::StatusRequest, expected);
}
Expand Down

0 comments on commit a9c5b2f

Please sign in to comment.