rtps_parser/rtps/messages/submessages/
info_destination.rs

1use crate::rtps::{
2    messages::{
3        overall_structure::{
4            RtpsMap, Submessage, SubmessageHeader, SubmessageHeaderRead, SubmessageHeaderWrite,
5        },
6        submessage_elements::SubmessageElement,
7        types::SubmessageKind,
8    },
9    types::GuidPrefix,
10};
11
12#[derive(Debug, PartialEq, Eq)]
13pub struct InfoDestinationSubmessageRead<'a> {
14    data: &'a [u8],
15}
16
17impl SubmessageHeader for InfoDestinationSubmessageRead<'_> {
18    fn submessage_header(&self) -> SubmessageHeaderRead {
19        SubmessageHeaderRead::new(self.data)
20    }
21}
22
23impl<'a> InfoDestinationSubmessageRead<'a> {
24    pub fn new(data: &'a [u8]) -> Self {
25        Self { data }
26    }
27
28    pub fn guid_prefix(&self) -> GuidPrefix {
29        self.map(&self.data[4..])
30    }
31}
32#[derive(Debug, PartialEq, Eq)]
33pub struct InfoDestinationSubmessageWrite<'a> {
34    submessage_elements: [SubmessageElement<'a>; 1],
35}
36
37impl InfoDestinationSubmessageWrite<'_> {
38    pub fn new(guid_prefix: GuidPrefix) -> Self {
39        Self {
40            submessage_elements: [SubmessageElement::GuidPrefix(guid_prefix)],
41        }
42    }
43}
44
45impl<'a> Submessage<'a> for InfoDestinationSubmessageWrite<'a> {
46    type SubmessageList = &'a [SubmessageElement<'a>];
47
48    fn submessage_header(&self, octets_to_next_header: u16) -> SubmessageHeaderWrite {
49        SubmessageHeaderWrite::new(SubmessageKind::INFO_DST, &[], octets_to_next_header)
50    }
51
52    fn submessage_elements(&'a self) -> Self::SubmessageList {
53        &self.submessage_elements
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use crate::rtps::{
61        messages::overall_structure::{into_bytes_vec, RtpsSubmessageWriteKind},
62        types::GUIDPREFIX_UNKNOWN,
63    };
64
65    #[test]
66    fn serialize_heart_beat() {
67        let guid_prefix = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
68        let submessage = RtpsSubmessageWriteKind::InfoDestination(
69            InfoDestinationSubmessageWrite::new(guid_prefix),
70        );
71        #[rustfmt::skip]
72        assert_eq!(into_bytes_vec(submessage), vec![
73              0x0e, 0b_0000_0001, 12, 0, // Submessage header
74                1, 2, 3, 4, //guid_prefix
75                5, 6, 7, 8, //guid_prefix
76                9, 10, 11, 12, //guid_prefix
77            ]
78        );
79    }
80
81    #[test]
82    fn deserialize_info_destination() {
83        #[rustfmt::skip]
84        let submessage = InfoDestinationSubmessageRead::new(&[
85            0x0e, 0b_0000_0001, 12, 0, // Submessage header
86            0, 0, 0, 0, //guid_prefix
87            0, 0, 0, 0, //guid_prefix
88            0, 0, 0, 0, //guid_prefix
89        ]);
90
91        let expected_guid_prefix = GUIDPREFIX_UNKNOWN;
92        assert_eq!(expected_guid_prefix, submessage.guid_prefix());
93    }
94}