use crate::propagation::PropagationCarrier;
use serde::Serialize;
use std::collections::BTreeMap;
#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum RabbitMqValue {
Void,
Bool(bool),
I8(i8),
U8(u8),
I16(i16),
U16(u16),
I32(i32),
U32(u32),
I64(i64),
F32(f32),
F64(f64),
Decimal {
scale: u8,
value: u32,
},
String(String),
Bytes(Vec<u8>),
Timestamp(u64),
Array(Vec<RabbitMqValue>),
Table(BTreeMap<String, RabbitMqValue>),
}
impl RabbitMqValue {
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(value) => Some(value),
_ => None,
}
}
}
impl From<&str> for RabbitMqValue {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}
impl From<String> for RabbitMqValue {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<bool> for RabbitMqValue {
fn from(value: bool) -> Self {
Self::Bool(value)
}
}
impl From<i64> for RabbitMqValue {
fn from(value: i64) -> Self {
Self::I64(value)
}
}
pub type RabbitMqHeaders = BTreeMap<String, RabbitMqValue>;
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct RabbitMqProperties {
pub content_type: Option<String>,
pub content_encoding: Option<String>,
pub priority: Option<u8>,
pub correlation_id: Option<String>,
pub reply_to: Option<String>,
pub expiration: Option<String>,
pub message_id: Option<String>,
pub timestamp: Option<u64>,
pub kind: Option<String>,
pub user_id: Option<String>,
pub app_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct RabbitMqMetadata {
pub queue: String,
pub exchange: String,
pub routing_key: String,
pub redelivered: bool,
pub persistent: bool,
pub delivery_tag: u64,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct RabbitMqRecord<T> {
pub value: T,
pub headers: RabbitMqHeaders,
pub properties: RabbitMqProperties,
pub metadata: RabbitMqMetadata,
}
impl<T> RabbitMqRecord<T> {
pub fn metadata(&self) -> &RabbitMqMetadata {
&self.metadata
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct RabbitMqPublish<T> {
pub value: T,
pub routing_key: Option<String>,
pub headers: RabbitMqHeaders,
pub properties: RabbitMqProperties,
}
impl<T> RabbitMqPublish<T> {
pub fn new(value: T) -> Self {
Self {
value,
routing_key: None,
headers: RabbitMqHeaders::new(),
properties: RabbitMqProperties::default(),
}
}
pub fn with_routing_key(mut self, routing_key: impl Into<String>) -> Self {
self.routing_key = Some(routing_key.into());
self
}
}
impl<T> PropagationCarrier for RabbitMqPublish<T> {
fn set_propagation_field(&mut self, name: &str, value: String) {
self.headers
.insert(name.to_owned(), RabbitMqValue::String(value));
}
}
#[cfg(feature = "testing")]
pub(crate) fn text_headers(headers: &RabbitMqHeaders) -> Vec<(&str, &str)> {
headers
.iter()
.filter_map(|(name, value)| Some((name.as_str(), value.as_str()?)))
.collect()
}