use crate::propagation::PropagationCarrier;
use serde::Serialize;
use std::{collections::BTreeMap, time::Duration};
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub enum SqsAttributeValue {
String(String),
Number(String),
Binary(Vec<u8>),
}
impl SqsAttributeValue {
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(value) => Some(value),
_ => None,
}
}
}
impl From<&str> for SqsAttributeValue {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}
impl From<String> for SqsAttributeValue {
fn from(value: String) -> Self {
Self::String(value)
}
}
pub type SqsAttributes = BTreeMap<String, SqsAttributeValue>;
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct SqsMetadata {
pub queue_url: String,
pub message_id: String,
pub receive_count: u32,
pub sent_timestamp: Option<u64>,
pub first_receive_timestamp: Option<u64>,
pub message_group_id: Option<String>,
pub deduplication_id: Option<String>,
pub sequence_number: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct SqsRecord<T> {
pub value: T,
pub attributes: SqsAttributes,
pub metadata: SqsMetadata,
}
impl<T> SqsRecord<T> {
pub fn metadata(&self) -> &SqsMetadata {
&self.metadata
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SqsPublish<T> {
pub value: T,
pub attributes: SqsAttributes,
pub message_group_id: Option<String>,
pub deduplication_id: Option<String>,
pub delay: Option<Duration>,
}
impl<T> SqsPublish<T> {
pub fn new(value: T) -> Self {
Self {
value,
attributes: SqsAttributes::new(),
message_group_id: None,
deduplication_id: None,
delay: None,
}
}
pub fn with_message_group_id(mut self, group: impl Into<String>) -> Self {
self.message_group_id = Some(group.into());
self
}
}
impl<T> PropagationCarrier for SqsPublish<T> {
fn set_propagation_field(&mut self, name: &str, value: String) {
self.attributes
.insert(name.to_owned(), SqsAttributeValue::String(value));
}
}
#[cfg(feature = "testing")]
pub(crate) fn text_attributes(attributes: &SqsAttributes) -> Vec<(&str, &str)> {
attributes
.iter()
.filter_map(|(name, value)| Some((name.as_str(), value.as_str()?)))
.collect()
}