|
13 | 13 |
|
14 | 14 | use std::str::FromStr; |
15 | 15 |
|
16 | | -use bytes::{Buf, BufMut, Bytes, BytesMut}; |
17 | 16 | use protobuf::{Enum, EnumOrUnknown, MessageField}; |
18 | 17 | use up_rust::{ |
19 | 18 | UAttributes, UAttributesValidators, UCode, UMessageType, UPayloadFormat, UPriority, UStatus, |
@@ -47,28 +46,6 @@ fn add_user_property( |
47 | 46 | .map_err(|e| UStatus::fail_with_code(UCode::INTERNAL, format!("{error_message}: {e:?}"))) |
48 | 47 | } |
49 | 48 |
|
50 | | -fn uuid_as_vec(uuid: &UUID) -> Vec<u8> { |
51 | | - let mut buf = BytesMut::with_capacity(16); |
52 | | - buf.put_u64(uuid.msb); |
53 | | - buf.put_u64(uuid.lsb); |
54 | | - buf.to_vec() |
55 | | -} |
56 | | - |
57 | | -fn uuid_from_bytes<B: Into<Bytes>>(bytes: B) -> Result<UUID, UStatus> { |
58 | | - let mut buf: Bytes = bytes.into(); |
59 | | - if buf.len() < 16 { |
60 | | - return Err(UStatus::fail_with_code( |
61 | | - UCode::INVALID_ARGUMENT, |
62 | | - "byte array must contain at least 16 bytes", |
63 | | - )); |
64 | | - } |
65 | | - Ok(UUID { |
66 | | - msb: buf.get_u64(), |
67 | | - lsb: buf.get_u64(), |
68 | | - ..Default::default() |
69 | | - }) |
70 | | -} |
71 | | - |
72 | 49 | /// Creates MQTT 5 header properties from uProtocol message meta data |
73 | 50 | /// as defined by uProtocol's MQTT5 transport specification. |
74 | 51 | /// |
@@ -198,10 +175,7 @@ pub(crate) fn create_mqtt_properties_from_uattributes( |
198 | 175 |
|
199 | 176 | if let Some(req_id) = attributes.reqid.as_ref() { |
200 | 177 | properties |
201 | | - .push_binary( |
202 | | - paho_mqtt::PropertyCode::CorrelationData, |
203 | | - uuid_as_vec(req_id), |
204 | | - ) |
| 178 | + .push_binary::<Vec<u8>>(paho_mqtt::PropertyCode::CorrelationData, req_id.into()) |
205 | 179 | .map_err(|e| { |
206 | 180 | UStatus::fail_with_code( |
207 | 181 | UCode::INTERNAL, |
@@ -379,7 +353,8 @@ pub(crate) fn create_uattributes_from_mqtt_properties( |
379 | 353 | } |
380 | 354 |
|
381 | 355 | if let Some(req_id) = props.get_binary(paho_mqtt::PropertyCode::CorrelationData) { |
382 | | - let uuid = uuid_from_bytes(req_id)?; |
| 356 | + let uuid = UUID::try_from(req_id) |
| 357 | + .map_err(|e| UStatus::fail_with_code(UCode::INVALID_ARGUMENT, e.to_string()))?; |
383 | 358 | attributes.reqid = Some(uuid).into(); |
384 | 359 | } |
385 | 360 |
|
@@ -624,10 +599,7 @@ mod tests { |
624 | 599 | } |
625 | 600 | if let Some(reqid_val) = reqid { |
626 | 601 | properties |
627 | | - .push_binary( |
628 | | - paho_mqtt::PropertyCode::CorrelationData, |
629 | | - uuid_as_vec(reqid_val), |
630 | | - ) |
| 602 | + .push_binary::<Vec<u8>>(paho_mqtt::PropertyCode::CorrelationData, reqid_val.into()) |
631 | 603 | .inspect_err(|err| println!("{err}")) |
632 | 604 | .expect("Failed to set Correlation Data property"); |
633 | 605 | } |
|
0 commit comments