rtps_parser/rtps/messages/submessages/
info_timestamp.rs1use crate::rtps::messages::{
2 overall_structure::{
3 RtpsMap, Submessage, SubmessageHeader, SubmessageHeaderRead, SubmessageHeaderWrite,
4 },
5 submessage_elements::SubmessageElement,
6 types::{SubmessageFlag, SubmessageKind, Time, TIME_INVALID},
7};
8
9#[derive(Debug, PartialEq, Eq)]
10pub struct InfoTimestampSubmessageRead<'a> {
11 data: &'a [u8],
12}
13
14impl SubmessageHeader for InfoTimestampSubmessageRead<'_> {
15 fn submessage_header(&self) -> SubmessageHeaderRead {
16 SubmessageHeaderRead::new(self.data)
17 }
18}
19
20impl<'a> InfoTimestampSubmessageRead<'a> {
21 pub fn new(data: &'a [u8]) -> Self {
22 Self { data }
23 }
24
25 pub fn invalidate_flag(&self) -> bool {
26 self.submessage_header().flags()[1]
27 }
28
29 pub fn timestamp(&self) -> Time {
30 if self.invalidate_flag() {
31 TIME_INVALID
32 } else {
33 self.map(&self.data[4..])
34 }
35 }
36}
37#[derive(Debug, PartialEq, Eq)]
38pub struct InfoTimestampSubmessageWrite<'a> {
39 pub invalidate_flag: SubmessageFlag,
40 timestamp_submessage_element: Option<SubmessageElement<'a>>,
41}
42
43impl InfoTimestampSubmessageWrite<'_> {
44 pub fn new(invalidate_flag: SubmessageFlag, timestamp: Time) -> Self {
45 let timestamp_submessage_element = if !invalidate_flag {
46 Some(SubmessageElement::Timestamp(timestamp))
47 } else {
48 None
49 };
50
51 Self {
52 invalidate_flag,
53 timestamp_submessage_element,
54 }
55 }
56}
57
58impl<'a> Submessage<'a> for InfoTimestampSubmessageWrite<'a> {
59 type SubmessageList = std::option::Iter<'a, SubmessageElement<'a>>;
60
61 fn submessage_header(&self, octets_to_next_header: u16) -> SubmessageHeaderWrite {
62 SubmessageHeaderWrite::new(
63 SubmessageKind::INFO_TS,
64 &[self.invalidate_flag],
65 octets_to_next_header,
66 )
67 }
68
69 fn submessage_elements(&'a self) -> Self::SubmessageList {
70 self.timestamp_submessage_element.iter()
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use crate::rtps::messages::overall_structure::{
78 into_bytes_vec, RtpsSubmessageWriteKind,
79 };
80
81 #[test]
82 fn serialize_info_timestamp_valid_time() {
83 let submessage = RtpsSubmessageWriteKind::InfoTimestamp(InfoTimestampSubmessageWrite::new(
84 false,
85 Time::new(4, 0),
86 ));
87 #[rustfmt::skip]
88 assert_eq!(into_bytes_vec(submessage), vec![
89 0x09_u8, 0b_0000_0001, 8, 0, 4, 0, 0, 0, 0, 0, 0, 0, ]
93 );
94 }
95
96 #[test]
97 fn serialize_info_timestamp_invalid_time() {
98 let submessage = RtpsSubmessageWriteKind::InfoTimestamp(InfoTimestampSubmessageWrite::new(
99 true,
100 TIME_INVALID,
101 ));
102 #[rustfmt::skip]
103 assert_eq!(into_bytes_vec(submessage), vec![
104 0x09_u8, 0b_0000_0011, 0, 0, ]
106 );
107 }
108
109 #[test]
110 fn deserialize_info_timestamp_valid_time() {
111 #[rustfmt::skip]
112 let submessage = InfoTimestampSubmessageRead::new(&[
113 0x09_u8, 0b_0000_0001, 8, 0, 4, 0, 0, 0, 0, 0, 0, 0, ]);
117
118 let expected_invalidate_flag = false;
119 let expected_timestamp = Time::new(4, 0);
120
121 assert_eq!(expected_invalidate_flag, submessage.invalidate_flag());
122 assert_eq!(expected_timestamp, submessage.timestamp());
123 }
124
125 #[test]
126 fn deserialize_info_timestamp_invalid_time() {
127 #[rustfmt::skip]
128 let submessage = InfoTimestampSubmessageRead::new(&[
129 0x09_u8, 0b_0000_0011, 0, 0, ]);
131
132 let expected_invalidate_flag = true;
133 let expected_timestamp = TIME_INVALID;
134
135 assert_eq!(expected_invalidate_flag, submessage.invalidate_flag());
136 assert_eq!(expected_timestamp, submessage.timestamp());
137 }
138}