|
| 1 | +//! Implements AMQP 1.0 binding for CloudEvents |
| 2 | +
|
| 3 | +use std::convert::TryFrom; |
| 4 | + |
| 5 | +use fe2o3_amqp_lib::types::messaging::{ApplicationProperties, Message, Body, Data as AmqpData, Properties}; |
| 6 | +use fe2o3_amqp_lib::types::primitives::{Value, Binary}; |
| 7 | + |
| 8 | +use crate::Event; |
| 9 | +use crate::message::Error; |
| 10 | + |
| 11 | +/// Type alias for an AMQP 1.0 message |
| 12 | +/// |
| 13 | +/// The generic parameter can be anything that implements `Serialize` and `Deserialize` but is of |
| 14 | +/// no importance because all CloudEvents are using the `Body::Data` as the body section type. For |
| 15 | +/// convenience, this type alias chose `Value` as the value of the generic parameter |
| 16 | +pub type AmqpMessage = Message<Value>; |
| 17 | + |
| 18 | +pub struct AmqpCloudEvent { |
| 19 | + properties: Properties, |
| 20 | + application_properties: ApplicationProperties, |
| 21 | + data: Binary, |
| 22 | +} |
| 23 | + |
| 24 | +impl From<AmqpCloudEvent> for AmqpMessage { |
| 25 | + fn from(event: AmqpCloudEvent) -> Self { |
| 26 | + Message::builder() |
| 27 | + .properties(event.properties) |
| 28 | + .application_properties(event.application_properties) |
| 29 | + .data(event.data) |
| 30 | + .build() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl TryFrom<AmqpMessage> for AmqpCloudEvent { |
| 35 | + type Error = Error; |
| 36 | + |
| 37 | + fn try_from(value: AmqpMessage) -> Result<Self, Self::Error> { |
| 38 | + let data = match value.body { |
| 39 | + Body::Data(AmqpData(data)) => data, |
| 40 | + _ => return Err(Error::WrongEncoding { }) |
| 41 | + }; |
| 42 | + let properties = value.properties |
| 43 | + .ok_or(Error::WrongEncoding { })?; |
| 44 | + let application_properties = value.application_properties |
| 45 | + .ok_or(Error::WrongEncoding { })?; |
| 46 | + Ok(Self { |
| 47 | + properties, |
| 48 | + application_properties, |
| 49 | + data, |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
0 commit comments