From 3ce72623e68a87f8bec8924b9dacf6751e13e781 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 3 Sep 2022 22:57:07 -0500 Subject: [PATCH] model: sns: Add union type The `model::sns::Message` enumeration provides a mechanism for deserializing a JSON document into the correct type. It will be used by the HTTP operation that receives messages from SNS in order to determine the correct action to take in response to the message. --- src/model/sns.rs | 19 +++++++++++++------ src/sns/sig.rs | 6 +++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/model/sns.rs b/src/model/sns.rs index 552fe9b..688cd25 100644 --- a/src/model/sns.rs +++ b/src/model/sns.rs @@ -6,6 +6,19 @@ //! [0]: https://docs.aws.amazon.com/sns/latest/dg/sns-message-and-json-formats.html use serde::Deserialize; +/// Union for all known SNS message types +/// +/// This type can be used to deserialize an arbitrary SNS message into the +/// appropriate type, based on the value of the `Type` field in the JSON +/// document. +#[derive(Deserialize)] +#[serde(tag = "Type")] +pub enum Message { + SubscriptionConfirmation(SubscriptionConfirmationMessage), + UnsubscribeConfirmation(UnsubscribeConfirmationMessage), + Notification(NotificationMessage), +} + /// SNS Subscription confirmation message /// /// See also: [HTTP/HTTPS subscription confirmation JSON format][0] @@ -14,8 +27,6 @@ use serde::Deserialize; #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] pub struct SubscriptionConfirmationMessage { - #[serde(rename = "Type")] - pub message_type: String, #[serde(rename = "MessageId")] pub message_id: String, pub token: String, @@ -38,8 +49,6 @@ pub struct SubscriptionConfirmationMessage { #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] pub struct NotificationMessage { - #[serde(rename = "Type")] - pub message_type: String, #[serde(rename = "MessageId")] pub message_id: String, pub topic_arn: String, @@ -61,8 +70,6 @@ pub struct NotificationMessage { #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] pub struct UnsubscribeConfirmationMessage { - #[serde(rename = "Type")] - pub message_type: String, #[serde(rename = "MessageId")] pub message_id: String, pub token: String, diff --git a/src/sns/sig.rs b/src/sns/sig.rs index 0624cb4..382e5b7 100644 --- a/src/sns/sig.rs +++ b/src/sns/sig.rs @@ -171,7 +171,7 @@ impl SignatureVerifier for SubscriptionConfirmationMessage { s.push_str(&self.topic_arn); s.push('\n'); s.push_str("Type\n"); - s.push_str(&self.message_type); + s.push_str("SubscriptionConfirmation"); s.push('\n'); s } @@ -206,7 +206,7 @@ impl SignatureVerifier for NotificationMessage { s.push_str(&self.topic_arn); s.push('\n'); s.push_str("Type\n"); - s.push_str(&self.message_type); + s.push_str("Notification"); s.push('\n'); s } @@ -242,7 +242,7 @@ impl SignatureVerifier for UnsubscribeConfirmationMessage { s.push_str(&self.topic_arn); s.push('\n'); s.push_str("Type\n"); - s.push_str(&self.message_type); + s.push_str("UnsubscribeConfirmation"); s.push('\n'); s }