use super::record::{
ServiceBusApplicationProperties, ServiceBusPublish, ServiceBusRecord, ServiceBusValue,
};
use crate::dead_letter::{DEAD_LETTER_HEADER_PREFIX, DeadLetter, DeadLetterDetails, parse_number};
use crate::error::Error;
const ORIGIN_ENTITY_PATH: &str = "beavers-dlq-origin-entity-path";
const ORIGIN_SEQUENCE_NUMBER: &str = "beavers-dlq-origin-sequence-number";
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServiceBusOrigin {
pub entity_path: String,
pub sequence_number: i64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServiceBusDeadLetter {
pub details: DeadLetterDetails,
pub origin: ServiceBusOrigin,
}
impl ServiceBusDeadLetter {
pub fn from_record<T>(record: &ServiceBusRecord<T>) -> Result<Option<Self>, Error> {
let field = |name: &str| match record.application_properties.get(name) {
None => Ok(None),
Some(value) => value.as_str().map(Some).ok_or_else(|| {
Error::invalid_record(format!(
"dead-letter application property {name} is not a string"
))
}),
};
let Some(details) = DeadLetterDetails::parse(field)? else {
return Ok(None);
};
let required = |name: &str| {
field(name)?.ok_or_else(|| {
Error::invalid_record(format!(
"dead-letter application property {name} is missing"
))
})
};
Ok(Some(Self {
details,
origin: ServiceBusOrigin {
entity_path: required(ORIGIN_ENTITY_PATH)?.to_owned(),
sequence_number: parse_number(
ORIGIN_SEQUENCE_NUMBER,
required(ORIGIN_SEQUENCE_NUMBER)?,
)?,
},
}))
}
}
impl ServiceBusPublish<Vec<u8>> {
pub fn from_dead_letter<I>(dead_letter: DeadLetter<I, ServiceBusRecord<Vec<u8>>>) -> Self {
let previous = ServiceBusDeadLetter::from_record(&dead_letter.raw)
.ok()
.flatten();
let details = DeadLetterDetails::of(&dead_letter, previous.as_ref().map(|p| &p.details));
let ServiceBusRecord {
value,
application_properties,
mut properties,
metadata,
} = dead_letter.raw;
let origin = previous.map_or(
ServiceBusOrigin {
entity_path: metadata.entity_path,
sequence_number: metadata.sequence_number,
},
|previous| previous.origin,
);
let mut application_properties: ServiceBusApplicationProperties = application_properties
.into_iter()
.filter(|(name, _)| !name.starts_with(DEAD_LETTER_HEADER_PREFIX))
.collect();
let origin_fields = [
(ORIGIN_ENTITY_PATH, origin.entity_path),
(ORIGIN_SEQUENCE_NUMBER, origin.sequence_number.to_string()),
];
application_properties.extend(
details
.fields()
.into_iter()
.chain(origin_fields)
.map(|(name, value)| (name.to_owned(), ServiceBusValue::String(value))),
);
properties.message_id = None;
properties.time_to_live = None;
Self {
value,
application_properties,
properties,
scheduled_enqueue_time: None,
}
}
}