Skip to content

Commit f1848c9

Browse files
committed
Serialize UUID to byte array using function from up-rust
Replaced custom code with standard library function.
1 parent cc74e11 commit f1848c9

1 file changed

Lines changed: 4 additions & 32 deletions

File tree

src/mapping.rs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use std::str::FromStr;
1515

16-
use bytes::{Buf, BufMut, Bytes, BytesMut};
1716
use protobuf::{Enum, EnumOrUnknown, MessageField};
1817
use up_rust::{
1918
UAttributes, UAttributesValidators, UCode, UMessageType, UPayloadFormat, UPriority, UStatus,
@@ -47,28 +46,6 @@ fn add_user_property(
4746
.map_err(|e| UStatus::fail_with_code(UCode::INTERNAL, format!("{error_message}: {e:?}")))
4847
}
4948

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-
7249
/// Creates MQTT 5 header properties from uProtocol message meta data
7350
/// as defined by uProtocol's MQTT5 transport specification.
7451
///
@@ -198,10 +175,7 @@ pub(crate) fn create_mqtt_properties_from_uattributes(
198175

199176
if let Some(req_id) = attributes.reqid.as_ref() {
200177
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())
205179
.map_err(|e| {
206180
UStatus::fail_with_code(
207181
UCode::INTERNAL,
@@ -379,7 +353,8 @@ pub(crate) fn create_uattributes_from_mqtt_properties(
379353
}
380354

381355
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()))?;
383358
attributes.reqid = Some(uuid).into();
384359
}
385360

@@ -624,10 +599,7 @@ mod tests {
624599
}
625600
if let Some(reqid_val) = reqid {
626601
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())
631603
.inspect_err(|err| println!("{err}"))
632604
.expect("Failed to set Correlation Data property");
633605
}

0 commit comments

Comments
 (0)