libits/transport/
packet.rs1#[cfg(feature = "telemetry")]
12use opentelemetry::propagation::{Extractor, Injector};
13
14use rumqttc::v5::mqttbytes::v5::PublishProperties;
15use std::fmt::Debug;
16
17use crate::transport::mqtt::topic::Topic;
18use crate::transport::payload::Payload;
19
20#[derive(Clone, Debug, Eq, PartialEq)]
21pub struct Packet<T, P>
22where
23 T: Topic,
24 P: Payload,
25{
26 pub topic: T,
27 pub payload: P,
28 pub properties: PublishProperties,
29}
30
31impl<T: Topic, P: Payload> Packet<T, P> {
32 pub fn new(topic: T, payload: P) -> Self {
33 Self {
34 topic,
35 payload,
36 properties: PublishProperties::default(),
37 }
38 }
39}
40
41#[cfg(feature = "telemetry")]
42impl<T: Topic, P: Payload> Injector for Packet<T, P> {
43 fn set(&mut self, key: &str, value: String) {
44 self.properties
45 .user_properties
46 .push((key.to_string(), value));
47 }
48}
49
50#[cfg(feature = "telemetry")]
51impl<T: Topic, P: Payload> Extractor for Packet<T, P> {
52 fn get(&self, key: &str) -> Option<&str> {
53 self.properties
54 .user_properties
55 .iter()
56 .find(|(k, _)| key == k)
57 .map(|(_, value)| value.as_str())
58 }
59
60 fn keys(&self) -> Vec<&str> {
61 self.properties
62 .user_properties
63 .iter()
64 .map(|(key, _)| key.as_str())
65 .collect::<Vec<&str>>()
66 }
67}